我實現了一個導航抽屜但我收到一個NullPointerException選擇一個項目後。當我點擊導航抽屜中的一個項目時,活動應該顯示帶有一些項目的ListView(「姓氏,名稱」)。導航抽屜空指針異常
PageOperations.java
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class PageUsersList extends Fragment
{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.pageuserslist, container, false);
return rootView;
}
}
pageuserslist.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/ListUsers"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
activity_main.xml中
enter code here
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ListView
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:choiceMode="singleChoice"
android:listSelector="@drawable/list_selector"
android:dividerHeight="0dp"
android:background="#111"/>
</android.support.v4.widget.DrawerLayout>
MainActivity.java
public class MainActivity extends Activity
{
...
private void selectItem(int position)
{
switch(position)
{
...
case 2:
Fragment fragment2 = new PageOperationsHistory();
Bundle args2 = new Bundle();
fragment2.setArguments(args2);
FragmentManager fragmentManager2 = getFragmentManager();
fragmentManager2.beginTransaction().replace(R.id.content_frame, fragment2).commit();
mDrawerList.setItemChecked(position, true);
mDrawerLayout.closeDrawer(mDrawerList);
PrintUtentList();
return;
...
}
}
...
private void PrintUtentList()
{
ListView ListUsers = (ListView) findViewById(R.id.ListUsers);
List<String> ArrayForListUsers = new ArrayList<String>();
for(int i=0;i<allNames.size();i++)
{
String value = allSurnNames.get(i).toString() + ", " +allNames.get(i).toString(); // allSurnNames and Names are ArrayList<String>
ArrayForListUsers.add(value); // All values are like "surname, name"
}
ListAdapter AdapterForListView = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_2, ArrayForListUsers);
ListUsers.setAdapter(AdapterForListView);
}
}
和
04-05 16:47:36.132: E/AndroidRuntime(2597): FATAL EXCEPTION: main
04-05 16:47:36.132: E/AndroidRuntime(2597): java.lang.NullPointerException
04-05 16:47:36.132: E/AndroidRuntime(2597): at com.example.apptesina.MainActivity.PrintUtentList(MainActivity.java:192)
04-05 16:47:36.132: E/AndroidRuntime(2597): at com.example.apptesina.MainActivity.selectItem(MainActivity.java:174)
04-05 16:47:36.132: E/AndroidRuntime(2597): at com.example.apptesina.MainActivity.access$0(MainActivity.java:143)
04-05 16:47:36.132: E/AndroidRuntime(2597): at com.example.apptesina.MainActivity$DrawerItemClickListener.onItemClick(MainActivity.java:139)
04-05 16:47:36.132: E/AndroidRuntime(2597): at android.widget.AdapterView.performItemClick(AdapterView.java:298)
04-05 16:47:36.132: E/AndroidRuntime(2597): at android.widget.AbsListView.performItemClick(AbsListView.java:1086)
04-05 16:47:36.132: E/AndroidRuntime(2597): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2859)
04-05 16:47:36.132: E/AndroidRuntime(2597): at android.widget.AbsListView$1.run(AbsListView.java:3533)
04-05 16:47:36.132: E/AndroidRuntime(2597): at android.os.Handler.handleCallback(Handler.java:615)
04-05 16:47:36.132: E/AndroidRuntime(2597): at android.os.Handler.dispatchMessage(Handler.java:92)
04-05 16:47:36.132: E/AndroidRuntime(2597): at android.os.Looper.loop(Looper.java:137)
04-05 16:47:36.132: E/AndroidRuntime(2597): at android.app.ActivityThread.main(ActivityThread.java:4745)
04-05 16:47:36.132: E/AndroidRuntime(2597): at java.lang.reflect.Method.invokeNative(Native Method)
04-05 16:47:36.132: E/AndroidRuntime(2597): at java.lang.reflect.Method.invoke(Method.java:511)
04-05 16:47:36.132: E/AndroidRuntime(2597): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
04-05 16:47:36.132: E/AndroidRuntime(2597): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-05 16:47:36.132: E/AndroidRuntime(2597): at dalvik.system.NativeStart.main(Native Method)
好,謝謝開始。那麼,我該如何解決? – 1megg1
@ 1megg1像'View rootView = inflater.inflate(R.layout.pageuserslist,container,false);'然後'ListView ListUsers =(ListView)rootView.findViewById(R.id.ListUsers);將其移動到片段'onCreateView';即使在這種情況下,' – Raghunandan