setAdapter() 上的 NullPointer 异常
Posted
技术标签:
【中文标题】setAdapter() 上的 NullPointer 异常【英文标题】:NullPointer Exception on setAdapter() 【发布时间】:2015-09-02 05:30:44 【问题描述】:这是我的第一个 android 应用程序,我要做的只是创建一个包含一些示例数据的列表。当我运行它时,我的手机上出现“不幸的阳光已停止”错误。
如果我删除这行代码,应用程序就会运行 -
lstview.setAdapter(mForecastAdapter);
另外,当我删除这行代码并运行应用程序时,我会得到一个历年的日历,但我的代码中没有任何地方使用过日历!知道为什么会这样吗?
这是我的 MainActivity.java -
public class MainActivity extends ActionBarActivity
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null)
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
@Override
public boolean onCreateOptionsMenu(Menu menu)
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
@Override
public boolean onOptionsItemSelected(MenuItem item)
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings)
return true;
return super.onOptionsItemSelected(item);
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment
private ArrayAdapter<String> mForecastAdapter;
public PlaceholderFragment()
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
String[] forecastArray;
forecastArray = new String[]
"Today - Sunny 88/63",
"Tomorrow - Foggy - 70/40",
"Thursday",
"Friday",
"Saturday",
"Sunday"
;
List<String> weekForecast = new ArrayList<String>(Arrays.asList(forecastArray));
mForecastAdapter = new
ArrayAdapter<String>(
getActivity(),
R.layout.list_item_forecast,
R.id.list_item_forecast_textview,
weekForecast);
ListView lstview = (ListView) rootView.findViewById(R.id.listview_forecast);
lstview.setAdapter(mForecastAdapter);
return rootView;
list_item_forecast.xml -
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
android:minHeight="?android:attr/listPreferredItemHeight"
android:gravity="center_vertical"
android:id="@+id/list_item_forecast_textview"
>
</TextView>
fragment_main.xml -
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_
android:layout_ android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity$PlaceholderFragment">
<ListView
android:layout_
android:layout_
android:id="@+id/listview_forecast"></ListView>
</FrameLayout>
AndroidManifest.xml -
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.prashanth.sunshine" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
这是一个非常简单的程序,我到处寻找,但似乎没有一个解决方案有效。所以——
当我删除这行代码时,为什么我的应用程序会出现无限日历?
lstview.setAdapter(mForecastAdapter);
为什么应用程序不使用该行运行?
顺便说一句,这里是 logcat -
06-17 11:34:32.121 32380-32380/com.example.prashanth.sunshine I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@1689948 time:428069386
06-17 11:36:28.478 641-641/com.example.prashanth.sunshine D/AndroidRuntime﹕ Shutting down VM
06-17 11:36:28.479 641-641/com.example.prashanth.sunshine E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.prashanth.sunshine, PID: 641
java.lang.RuntimeException: Unable to start activity ComponentInfocom.example.prashanth.sunshine/com.example.prashanth.sunshine.MainActivity: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2354)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.access$900(ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1319)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5291)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:931)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:726)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at com.example.prashanth.sunshine.MainActivity$PlaceholderFragment.onCreateView(MainActivity.java:90)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:953)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1136)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1499)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:548)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1236)
at android.app.Activity.performStart(Activity.java:6062)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2317)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.access$900(ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1319)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5291)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:931)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:726)
06-17 11:42:14.846 1437-1437/com.example.prashanth.sunshine I/art﹕ Late-enabling -Xcheck:jni
06-17 11:42:15.308 1437-1437/com.example.prashanth.sunshine D/AndroidRuntime﹕ Shutting down VM
06-17 11:42:15.312 1437-1437/com.example.prashanth.sunshine E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.prashanth.sunshine, PID: 1437
java.lang.RuntimeException: Unable to start activity ComponentInfocom.example.prashanth.sunshine/com.example.prashanth.sunshine.MainActivity: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2354)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.access$900(ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1319)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5291)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:931)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:726)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at com.example.prashanth.sunshine.MainActivity$PlaceholderFragment.onCreateView(MainActivity.java:91)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:953)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1136)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1499)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:548)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1236)
at android.app.Activity.performStart(Activity.java:6062)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2317)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.access$900(ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1319)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5291)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:931)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:726)
06-17 11:51:54.224 2292-2292/com.example.prashanth.sunshine D/AndroidRuntime﹕ Shutting down VM
06-17 11:51:54.225 2292-2292/com.example.prashanth.sunshine E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.prashanth.sunshine, PID: 2292
java.lang.RuntimeException: Unable to start activity ComponentInfocom.example.prashanth.sunshine/com.example.prashanth.sunshine.MainActivity: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2354)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.access$900(ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1319)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5291)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:931)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:726)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at com.example.prashanth.sunshine.MainActivity$PlaceholderFragment.onCreateView(MainActivity.java:91)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:953)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1136)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1499)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:548)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1236)
at android.app.Activity.performStart(Activity.java:6062)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2317)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.access$900(ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1319)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5291)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:931)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:726)
06-17 11:51:56.581 2292-2292/com.example.prashanth.sunshine I/Process﹕ Sending signal. PID: 2292 SIG: 9
编辑:Activity_main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container"
android:layout_ android:layout_
tools:context=".MainActivity" tools:ignore="MergeRootFrame" />
提前致谢!
【问题讨论】:
MainActivity.java 中的第 90 行是什么? 你在哪里声明这个对象? mForecastAdapter 我知道错误不在activity_main.xml中,但你能把它贴在这里吗。 我已经在这一行声明了 mForecastAdapter - public static class PlaceholderFragment extends Fragment private ArrayAdapter我更改了您的代码的某些部分,它似乎可以正常工作。我相信这是错误导入 AppCompat 库的问题。下面是整个 MainActivity 代码。
import android.app.Fragment;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class MainActivity extends ActionBarActivity
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null)
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment
private ArrayAdapter<String> mForecastAdapter;
public PlaceholderFragment()
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
String[] forecastArray;
forecastArray = new String[]
"Today - Sunny 88/63",
"Tomorrow - Foggy - 70/40",
"Day After - Life",
"Kidney",
"Saturday",
"Sunday"
;
List<String> weekForecast = new ArrayList<String>(Arrays.asList(forecastArray));
mForecastAdapter = new
ArrayAdapter<String>(
getActivity(),
R.layout.list_item_forecast,
weekForecast);
ListView lstview = (ListView) rootView.findViewById(R.id.listview_forecast);
lstview.setAdapter(mForecastAdapter);
return rootView;
这是你要找的输出吗
【讨论】:
是的,这正是我正在寻找的输出!但是您粘贴的代码仍然给我同样的“不幸的是阳光已经停止”错误。你做了哪些改变?谢谢! 不多。比较我的导入语句和你的。我认为这就是导致问题的原因。虽然我不确定。我相信我将 getSupportFragmentManager() 更改为 getFragmentManager() 和一些导入语句 我的导入和你的一样,但仍然无法运行。如果我删除此语句,它会运行并显示日历(为什么?) - lstview.setAdapter(mForecastAdapter);有什么想法吗? 我不知道。但它如何显示日历?你说的日历是什么意思。尝试清理项目并重建。除此之外。我很无奈。 由于某种原因,它会显示全年的日历以及月份和日期。我不知道为什么会这样。无论如何感谢您的帮助,我会尝试清洁它!以上是关于setAdapter() 上的 NullPointer 异常的主要内容,如果未能解决你的问题,请参考以下文章
带有 ListView 的片段:setAdapter 上的 NullPointerException
doFilter() 重写方法 httpRequest.getUserPrincipal() 总是返回 NullPoint 异常
Android内存重启之静态变量被回收导致nullPoint问题
setAdapter“调用需要 API 级别 11(当前最低为 8):android.widget.AbsListView#setAdapter”?