我正在开发一个我有4个片段的应用。现在我想在片段上添加listview。我尝试过谷歌的各种链接但是还没有能够做到这一点。
我的片段代码类:
public class ExerciseActivity extends Fragment {
public static final String ARG_SECTION_NUMBER = "section_number";
public ExerciseActivity() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Create a new TextView and set its text to the fragment's section
// number argument value.
View V = inflater.inflate(R.layout.fragment_exercise,container,false);
//View V = inflater.inflate(R.layout.mainList, container, false);
ListView LV = (ListView) this.getActivity().findViewById(R.id.mainList);
String List[] = new String[2];
List[0] = "tst1";
List[1] ="tst2";
ArrayAdapter<String> aa = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, List);
LV.setAdapter(aa);
return V;
}
}
以下是我的片段xml代码:
<?xml version="1.0" encoding="utf-8"?>
每当我尝试运行我的应用程序时,我都会收到以下错误:
05-29 00:11:42.680:E / AndroidRuntime(1298):致命异常:主要 05-29 00:11:42.680:E / AndroidRuntime(1298):java.lang.RuntimeException:无法启动活动ComponentInfo {com.kris.fittracker / com.kris.fittracker.MainActivity}:java.lang.NullPointerException 05-29 00:11:42.680:E / AndroidRuntime(1298):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955) 05-29 00:11:42.680:E / AndroidRuntime(1298):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980) 05-29 00:11:42.680:E / AndroidRuntime(1298):在android.app.ActivityThread.access $ 600(ActivityThread.java:122) 05-29 00:11:42.680:E / AndroidRuntime(1298):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1146) 05-29 00:11:42.680:E / AndroidRuntime(1298):在android.os.Handler.dispatchMessage(Handler.java:99) 05-29 00:11:42.680:E / AndroidRuntime(1298):在android.os.Looper.loop(Looper.java:137) 05-29 00:11:42.680:E / AndroidRuntime(1298):在android.app.ActivityThread.main(ActivityThread.java:4340) 05-29 00:11:42.680:E / AndroidRuntime(1298):at java.lang.reflect.Method.invokeNative(Native Method) 05-29 00:11:42.680:E / AndroidRuntime(1298):at java.lang.reflect.Method.invoke(Method.java:511) 05-29 00:11:42.680:E / AndroidRuntime(1298):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:784) 05-29 00:11:42.680:E / AndroidRuntime(1298):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 05-29 00:11:42.680:E / AndroidRuntime(1298):at dalvik.system.NativeStart.main(Native Method) 05-29 00:11:42.680:E / AndroidRuntime(1298):引起:java.lang.NullPointerException 05-29 00:11:42.680:E / AndroidRuntime(1298):at com.kris.fittracker.ExerciseActivity.onCreateView(ExerciseActivity.java:31) 05-29 00:11:42.680:E / AndroidRuntime(1298):在android.support.v4.app.Fragment.performCreateView(Fragment.java:1500) 05-29 00:11:42.680:E / AndroidRuntime(1298):在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:938) 05-29 00:11:42.680:E / AndroidRuntime(1298):在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115) 05-29 00:11:42.680:E / AndroidRuntime(1298):在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1097) 05-29 00:11:42.680:E / AndroidRuntime(1298):在android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1895) 05-29 00:11:42.680:E / AndroidRuntime(1298):在android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:566) 05-29 00:11:42.680:E / AndroidRuntime(1298):在android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1133) 05-29 00:11:42.680:E / AndroidRuntime(1298):在android.app.Activity.performStart(Activity.java:4475) 05-29 00:11:42.680:E / AndroidRuntime(1298):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1928)
答案 0 :(得分:2)
问题是:
ListView LV = (ListView) this.getActivity().findViewById(R.id.mainList);
getActivity()上下文不了解 mainList ,您需要使用包含带有该ID的ListView的正确查看:
ListView LV = (ListView) V.findViewById(R.id.mainList);
我正在谈论的是你的视图命名为 V 。现在它应该有效。
答案 1 :(得分:1)
如果您的列表视图包含在R.layout.fragment_exercise布局文件中,请将listview映射更改为
ListView LV = (ListView)V.findViewById(R.id.mainList);
答案 2 :(得分:1)
问题在于您从主要活动布局获取视图。
ListView LV = (ListView) this.getActivity().findViewById(R.id.mainList);
this.getAcitivity是主要的活动..
<强>溶液强>
使用刚刚膨胀的视图
ListView LV = (ListView) V.findViewById(R.id.mainList);
答案 3 :(得分:0)
我的完整代码(listview在上面声明为&#34;私有ListView listView;&#34;)
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
v = inflater.inflate(R.layout.second_fragment, container, false);
listView = (ListView) v.findViewById(R.id.times_listView);
return v;
}