我正在尝试使用自定义列表视图实现List片段。实现我在logcat中遇到错误。我试图在我的片段onCreateView()中获取Listview像这样
ArrayListFragment list = new ArrayListFragment();
getActivity().getSupportFragmentManager().beginTransaction().add(android.R.id.content,list).commit();
我没有弄到它有什么问题。
我的自定义listfragment类是: -
public static class ArrayListFragment extends ListFragment implements OnClickListener{
PairWithSensorFragment mPairWithSensorFragment = new PairWithSensorFragment();
String[] arrayValues = {"Android", "BlackBerry", "iPhone", "Windows", "linuxPhone"};
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
R.layout.pairwithsensor_listrow, android.R.id.text1, arrayValues);
setListAdapter(adapter);
/* ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, arrayValues);
setListAdapter(adapter);*/
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// Do something with the data
Toast.makeText( getActivity().getBaseContext() , arrayValues[position]+" "+"Clicked " , Toast.LENGTH_SHORT).show();
}
}
pairwithsensor_listrow.xml: -
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/linear_layout">
<TextView
android:id="@+id/tv_pairwithsensor_sensorid"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_alignParentLeft="true">
</TextView>
<EditText
android:id="@+id/edt_pairwithsensor_sensorName"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_toRightOf="@id/tv_pairwithsensor_sensorid"
android:hint="@string/please_enter_value">
</EditText>
<Button
android:id="@+id/btn_pairwithsensor_pair"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_alignParentRight="true"
android:text="@string/PairwithSensor_pairButton"/>
</RelativeLayout>
Logcat
02-25 10:22:22.870: E/AndroidRuntime(10247): FATAL EXCEPTION: main
02-25 10:22:22.870: E/AndroidRuntime(10247): java.lang.NullPointerException
02-25 10:22:22.870: E/AndroidRuntime(10247): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:392)
02-25 10:22:22.870: E/AndroidRuntime(10247): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
02-25 10:22:22.870: E/AndroidRuntime(10247): at android.widget.AbsListView.obtainView(AbsListView.java:2033)
02-25 10:22:22.870: E/AndroidRuntime(10247): at android.widget.ListView.makeAndAddView(ListView.java:1772)
02-25 10:22:22.870: E/AndroidRuntime(10247): at android.widget.ListView.fillDown(ListView.java:672)
02-25 10:22:22.870: E/AndroidRuntime(10247): at android.widget.ListView.fillFromTop(ListView.java:732)
02-25 10:22:22.870: E/AndroidRuntime(10247): at android.widget.ListView.layoutChildren(ListView.java:1625)
02-25 10:22:22.870: E/AndroidRuntime(10247): at android.widget.AbsListView.onLayout(AbsListView.java:1863)
02-25 10:22:22.870: E/AndroidRuntime(10247): at android.view.View.layout(View.java:11282)
02-25 10:22:22.870: E/AndroidRuntime(10247): at android.view.ViewGroup.layout(ViewGroup.java:4224)
02-25 10:22:22.870: E/AndroidRuntime(10247): at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
02-25 10:22:22.870: E/AndroidRuntime(10247): at android.view.View.layout(View.java:11282)
02-25 10:22:22.870: E/AndroidRuntime(10247): at android.view.ViewGroup.layout(ViewGroup.java:4224)
02-25 10:22:22.870: E/AndroidRuntime(10247): at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
02-25 10:22:22.870: E/AndroidRuntime(10247): at android.view.View.layout(View.java:11282)
02-25 10:22:22.870: E/AndroidRuntime(10247): at android.view.ViewGroup.layout(ViewGroup.java:4224)
02-25 10:22:22.870: E/AndroidRuntime(10247): at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
02-25 10:22:22.870: E/AndroidRuntime(10247): at android.view.View.layout(View.java:11282)
02-25 10:22:22.870: E/AndroidRuntime(10247): at android.view.ViewGroup.layout(ViewGroup.java:4224)
02-25 10:22:22.870: E/AndroidRuntime(10247): at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
02-25 10:22:22.870: E/AndroidRuntime(10247): at android.view.View.layout(View.java:11282)
02-25 10:22:22.870: E/AndroidRuntime(10247): at android.view.ViewGroup.layout(ViewGroup.java:4224)
02-25 10:22:22.870: E/AndroidRuntime(10247): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628)
02-25 10:22:22.870: E/AndroidRuntime(10247): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1486)
02-25 10:22:22.870: E/AndroidRuntime(10247): at android.widget.LinearLayout.onLayout(LinearLayout.java:1399)
02-25 10:22:22.870: E/AndroidRuntime(10247): at android.view.View.layout(View.java:11282)
02-25 10:22:22.870: E/AndroidRuntime(10247): at android.view.ViewGroup.layout(ViewGroup.java:4224)
02-25 10:22:22.870: E/AndroidRuntime(10247): at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
02-25 10:22:22.870: E/AndroidRuntime(10247): at android.view.View.layout(View.java:11282)
02-25 10:22:22.870: E/AndroidRuntime(10247): at android.view.ViewGroup.layout(ViewGroup.java:4224)
02-25 10:22:22.870: E/AndroidRuntime(10247): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1514)
02-25 10:22:22.870: E/AndroidRuntime(10247): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2467)
02-25 10:22:22.870: E/AndroidRuntime(10247): at android.os.Handler.dispatchMessage(Handler.java:99)
02-25 10:22:22.870: E/AndroidRuntime(10247): at android.os.Looper.loop(Looper.java:137)
02-25 10:22:22.870: E/AndroidRuntime(10247): at android.app.ActivityThread.main(ActivityThread.java:4424)
02-25 10:22:22.870: E/AndroidRuntime(10247): at java.lang.reflect.Method.invokeNative(Native Method)
02-25 10:22:22.870: E/AndroidRuntime(10247): at java.lang.reflect.Method.invoke(Method.java:511)
02-25 10:22:22.870: E/AndroidRuntime(10247): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-25 10:22:22.870: E/AndroidRuntime(10247): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-25 10:22:22.870: E/AndroidRuntime(10247): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
看看这个:ArrayAdapter(Context context, int layoutId, int textViewResourceId, T[] objects)
textViewResourceId
应该是布局中TextView的资源ID,例如R.id.tv_pairwithsensor_sensorid
。并且您指定了android.R.id.text1
(可能来自使用标准布局的示例)。例外情况只是告诉你找不到你指定的id的TextView。