这让我现在正在崛起。我有两个带有ListView的片段,它们都使用ButterKnife
。其中一个工作得很好,但另一个没赢。
这是片段:
//IRRELEVANT CODE ABOVE
@BindView(R.id.exerciseListView1)
ListView exerciseListView;
/**
* Mandatory empty constructor for the fragment manager to instantiate the
* fragment (e.g. upon screen orientation changes).
*/
public ExerciseFragment() {
}
@SuppressWarnings("unused")
public static ExerciseFragment newInstance(int columnCount) {
ExerciseFragment fragment = new ExerciseFragment();
Bundle args = new Bundle();
args.putInt(ARG_COLUMN_COUNT, columnCount);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mColumnCount = getArguments().getInt(ARG_COLUMN_COUNT);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_exercise_list, container, false);
unbinder = ButterKnife.bind(this, view);
ListItem[] exercises = {
new ListItem(" 1. Learning Names & Faces", "This exercise will teach you how to learn people's names."),
new ListItem(" 2. Paying Attention", "This exercise will test how well you pay attention."),
new ListItem(" 3. Memory Recall", "This exercise will help you improve your recall skills.")
};
mAdapter = new ArrayAdapter<ListItem>(view.getContext(), R.layout.mylist, exercises);
exerciseListView.setAdapter(mAdapter);
exerciseListView.setOnItemClickListener(new ListView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String item = ((TextView) view).getText().toString();
Log.d("ExercisesActivity", item + " #" + Integer.toString(position) + " selected");
//Switch statements won't take longs, thankfully there's the position variable
switch (position) {
case 0:
startActivity(new Intent(getContext(), LearningNamesActivity.class));
break;
case 1:
startActivity(new Intent(getContext(), PayingAttentionActivity.class));
break;
case 2:
startActivity(new Intent(getContext(), MemoryRecallActivity.class));
break;
}
}
});
return view;
}
这是一个不会工作的人:
//IRRELEVANT CODE ABOVE
@BindView(R.id.informationListView)
ListView informationList;
/**
* Mandatory empty constructor for the fragment manager to instantiate the
* fragment (e.g. upon screen orientation changes).
*/
public InformationFragment() {
}
// TODO: Customize parameter initialization
@SuppressWarnings("unused")
public static InformationFragment newInstance(int columnCount) {
InformationFragment fragment = new InformationFragment();
Bundle args = new Bundle();
args.putInt(ARG_COLUMN_COUNT, columnCount);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mColumnCount = getArguments().getInt(ARG_COLUMN_COUNT);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_information_list, container, false);
unbinder = ButterKnife.bind(getContext(), view);
ListItem[] exercises = {
new ListItem("1. Tell me about memory", ""),
new ListItem("2. What can go wrong with my memory", ""),
new ListItem("3. Helpful tips to improve my memory", "")
};
mAdapter = new ArrayAdapter<>(view.getContext(), R.layout.mylist, exercises);
informationList.setAdapter(mAdapter);
informationList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
case 0:
startActivity(new Intent(getContext(), LearnAboutMemoryActivity.class));
break;
case 1:
startActivity(new Intent(getContext(), WhatCanGoWrongActivity.class));
break;
case 2:
//TODO: Use HelpfulTipsActivity as an "all-purpose" activity for holding tip Fragments
startActivity(new Intent(getContext(), HelpfulTipsActivity.class));
break;
}
}
});
return view;
这里是错误日志:
06-17 12:20:15.674 13182-13182/com.tommy.mastersproject E/AndroidRuntime: FATAL EXCEPTION: main Process: com.tommy.mastersproject, PID: 13182
Theme: themes:{com.android.settings=overlay:com.cyngn.hexo, default=overlay:system, iconPack:system, fontPkg:system, com.android.systemui=overlay:system, com.android.systemui.navbar=overlay:system}
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tommy.mastersproject/com.tommy.mastersproject.activities.InformationActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2450)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2510)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5461)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at com.tommy.mastersproject.fragments.InformationFragment.onCreateView(InformationFragment.java:90)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:742)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:339)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:601)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1238)
at android.app.Activity.performStart(Activity.java:6268)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2413)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2510)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5461)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
这里是碎片&#39;各个布局文件: 正常工作
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/exerciseListView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
无法正常工作
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/informationListView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
工作片段中有什么东西我忘了放入那个不起作用的东西吗?
答案 0 :(得分:1)
我认为问题出在InformationFragment
内onCreateView()
:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_information_list, container, false);
unbinder = ButterKnife.bind(this, view);
ListItem[] exercises = {
new ListItem("1. Tell me about memory", ""),
new ListItem("2. What can go wrong with my memory", ""),
new ListItem("3. Helpful tips to improve my memory", "")
};
// rest of your code
<强>问题:强>
在绑定视图时,您使用的是
getContext()
。所以您的informationList
ListView仍为 Null ,您获得了NullPointerException
移除getContext()
,然后添加this
。