我不知道该怎么做。应用程序一直在崩溃,经过几个小时的搜索后,我什么都没得到。有谁知道问题在哪里?
以下是我声明的所有变量:
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
public ArrayList<String> numberList = new ArrayList<String>();
public ListView listOfItems;
这是 onCreate 功能:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
listOfItems = (ListView) findViewById(R.id.listView);
setContentView(R.layout.activity_main);
numberList.add("Hello");
numberList.add("World");
StableArrayAdapter listAdapter = new StableArrayAdapter(this, android.R.layout.simple_list_item_1, numberList);
listOfItems.setAdapter(listAdapter);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
[ EDIT ]这是 fragment_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" 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_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/addButton" />
</RelativeLayout>
它还提供以下错误日志:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.adnan.figura/com.adnan.figura.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:2693)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758)
at android.app.ActivityThread.access$900(ActivityThread.java:177)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5942)
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:1388)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)
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.adnan.figura.MainActivity.onCreate(MainActivity.java:63)
at android.app.Activity.performCreate(Activity.java:6289)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758)
at android.app.ActivityThread.access$900(ActivityThread.java:177)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5942)
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:1388)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)
答案 0 :(得分:1)
您应该初始化setContentView。下面的listOfItems。
应该是这样的:
的setContentView(R.layout.activity_main);
listOfItems =(ListView)findViewById(R.id.listView);
并检查R.layout.activity_main
上是否存在R.id.listView答案 1 :(得分:1)
你必须说
setContentView(R.layout.activity_main);
首先是,然后是ListView的findViewById。
答案 2 :(得分:1)
你有一个非常基本的错误,可以在几秒钟内解决。
请将两行的顺序更改为此。
setContentView(R.layout.activity_main);
listOfItems = (ListView) findViewById(R.id.listView);
首先需要设置内容,然后创建视图,然后使用findViewById()。
您实际上是在创建视图之前尝试查找视图。希望我能解决你的问题并解释你的错误。