我在列表视图中的 setAdapter 处收到 NullPointerException。如果有人可以帮助我,我将不胜感激。我的文件如下:
MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create the adapter that will return a fragment for each of the three
// primary sections of the app.
mSectionsPagerAdapter = new SectionsPagerAdapter(
getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
MainActivityContext=this;
callGeolocationApi1();
ListView categoryList = (ListView) this.findViewById(R.id.listView);
CategoryAdapter categoryAdapter = new CategoryAdapter(this, R.layout.category2_row_layout,
getCategoriesList());
//java.lang.NullPointerException occurs here
categoryList.setAdapter(categoryAdapter);
}
private List<String> getCategoriesList() {
List<String> strlist = new ArrayList<String>();
strlist.add("str1");
strlist.add("str2");
return strlist;
}
fragment_main_dummy.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout
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:orientation="vertical"
android:background="@color/orange" tools:context=".MainActivity">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listView"
android:layout_gravity="center"
tools:listitem="@layout/category2_row_layout"/>
category2_row_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="6dip"
android:background="#00000000">
<TextView
android:id="@+id/category_name"
android:layout_width="200dip"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_marginTop="10dip"
android:scrollbars="vertical"
android:fadingEdge="vertical"
android:layout_alignParentLeft="true"
android:background="#00000000"
/>
<ImageButton
android:id="@+id/category_delete_button"
android:background="#00000000"
android:src="@drawable/settings"
android:layout_width="50dip"
android:layout_height="50dip"
android:scrollbars="vertical"
android:fadingEdge="vertical"
android:layout_alignParentRight="true"
android:focusable="false"
android:focusableInTouchMode="false"
/>
</RelativeLayout>
Logcat - http://pastebin.com/phLYcPC8