我实现了导航抽屉但是在选择项目后我收到了 NullPointerException 。当我单击导航抽屉中的某个项目时,Activity将显示一个带有一些项目的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)
答案 0 :(得分:0)
ListView
<ListView
android:id="@+id/ListUsers" // does not belong to activity layout
位于pageuserslist.xml
,属于片段
但在活动中你有
ListView ListUsers = (ListView) findViewById(R.id.ListUsers);
导致NullPointerException
。
因此,您需要在片段onCreateView
中初始化listview答案 1 :(得分:0)
PrintUtentList()
方法属于PageOperationsHistory
片段,而不属于您的活动。您应该在片段onActivityCreated()
方法中调用它。
顺便说一句,方法和变量名称通常以Java中的小写开头