在ListFragment内的适配器上调用notifyDataSetChanged()会导致NullPointerException? [英] Calling notifyDataSetChanged() on my adapter that is within a ListFragment causes a NullPointerException?
问题描述
简介:
我进行了以下设置:
-
使用方法
onNewIntent
和processNewIntent
的MainActivity
MainActivity
还实现了FragmentActivity
;并具有实现了FragmentList
A
MainActivity
with methodsonNewIntent
andprocessNewIntent
, theMainActivity
also implementsFragmentActivity
; and has a tabbed fragment which implementsFragmentList
A listaddactivity $>的选项卡式片段c $ c>活动,该活动创建一个新的
Parcelable
listControlObject
A listaddactivity
activity that creates a new Parcelable
listControlObject
listaddactivity
活动发送一个意图,该意图携带一个包裹,然后在 MainActivity
'内部重构listControlObject。 s onNewIntent
和 processNewIntent
类。
The listaddactivity
activity sends an intent carrying a parcel which then reconstructs the listControlObject inside of the MainActivity
's onNewIntent
and processNewIntent
class.
然而;我可以将 intent
放入我的 onNewIntent
方法中,并构造新的 listControlObject
罚款;但是当我在 FragmentList
类上调用 adapter.notifyDataSetChanged
方法时。
However; I can take in the intent
into my onNewIntent
method and it constructs the new listControlObject
fine; but when I call the adapter.notifyDataSetChanged
method on my FragmentList
class.
Logcat错误:
03-24 04:18:44.149: E/Trace(16964): error opening trace file: No such file or directory (2)
03-24 04:18:49.543: E/AndroidRuntime(16964): FATAL EXCEPTION: main
03-24 04:18:49.543: E/AndroidRuntime(16964): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nanospark.upcdemo/com.nanospark.upcdemo.MainActivity}: java.lang.NullPointerException
03-24 04:18:49.543: E/AndroidRuntime(16964): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
03-24 04:18:49.543: E/AndroidRuntime(16964): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2211)
03-24 04:18:49.543: E/AndroidRuntime(16964): at android.app.ActivityThread.access$600(ActivityThread.java:149)
03-24 04:18:49.543: E/AndroidRuntime(16964): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1300)
03-24 04:18:49.543: E/AndroidRuntime(16964): at android.os.Handler.dispatchMessage(Handler.java:99)
03-24 04:18:49.543: E/AndroidRuntime(16964): at android.os.Looper.loop(Looper.java:153)
03-24 04:18:49.543: E/AndroidRuntime(16964): at android.app.ActivityThread.main(ActivityThread.java:4987)
03-24 04:18:49.543: E/AndroidRuntime(16964): at java.lang.reflect.Method.invokeNative(Native Method)
03-24 04:18:49.543: E/AndroidRuntime(16964): at java.lang.reflect.Method.invoke(Method.java:511)
03-24 04:18:49.543: E/AndroidRuntime(16964): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
03-24 04:18:49.543: E/AndroidRuntime(16964): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
03-24 04:18:49.543: E/AndroidRuntime(16964): at dalvik.system.NativeStart.main(Native Method)
03-24 04:18:49.543: E/AndroidRuntime(16964): Caused by: java.lang.NullPointerException
03-24 04:18:49.543: E/AndroidRuntime(16964): at com.nanospark.upcdemo.MainActivity.processNewIntent(MainActivity.java:102)
03-24 04:18:49.543: E/AndroidRuntime(16964): at com.nanospark.upcdemo.MainActivity.onCreate(MainActivity.java:81)
03-24 04:18:49.543: E/AndroidRuntime(16964): at android.app.Activity.performCreate(Activity.java:5020)
03-24 04:18:49.543: E/AndroidRuntime(16964): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
03-24 04:18:49.543: E/AndroidRuntime(16964): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
03-24 04:18:49.543: E/AndroidRuntime(16964): ... 11 more
The onNewIntent
方法:
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
processNewIntent(intent);
}
processNewIntent
方法:
private void processNewIntent(Intent intent) {
cyclefragment = new cycleviewfragment();
Log.d("TEST", "METHOD CALLED TEST");
if (intent.hasExtra("custom_object")) {
if (intent.getParcelableExtra("custom_object").equals(null)) {
Log.d("Testing null", "Object is null");
} else {
Log.d("Testing null", "Object is not null");
cyclefragment.getA1().add((listControlObject)
intent.getParcelableExtra("custom_object"));
cyclefragment.adapter.notifyDataSetChanged();
}
}
抛出nullpointerexception的特定行:
The specific line that throws the nullpointerexception:
cyclefragment.adapter.notifyDataSetChanged(); // - THIS IS LINE 102
ListFragment
类:
public class cycleviewfragment extends ListFragment {
public View cycleviewfragment;
public ArrayList<listControlObject> a1 = new ArrayList<listControlObject>();
public ArrayAdapter<listControlObject> adapter;
public Button addButton;
public Context context;
public View onCreateView(LayoutInflater viewInflation, ViewGroup container,
Bundle SavedInstantState) {
cycleviewfragment = viewInflation.inflate(
R.layout.cycleviewfragment_page, container, false);
context = getActivity().getApplicationContext();
a1.add(new listControlObject(1, "Name", 1, 1, 1, 1, 1, 1, 1, 1, 1));
adapter = new ArrayAdapter<listControlObject>(getActivity()
.getApplicationContext(), android.R.layout.simple_list_item_1,
a1);
this.setListAdapter(adapter);
addButton = (Button) cycleviewfragment.findViewById(R.id.addbutton);
addButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent myIntent = new Intent(getActivity(),
listaddactivity.class);
// myIntent.putExtra("Array", a1); // Optional parameters
getActivity().startActivity(myIntent);
}
});
return cycleviewfragment;
}
public ArrayAdapter<listControlObject> getAdapter() {
return adapter;
}
public void setAdapter(ArrayAdapter<listControlObject> adapter) {
this.adapter = adapter;
}
public ArrayList<listControlObject> getA1() {
return a1;
}
public void setA1(ArrayList<listControlObject> a1) {
this.a1 = a1;
}
}
推荐答案
尝试
if (cyclefragment != null && cyclefragment.adapter != null) {
cyclefragment.adapter.notifyDataSetChanged();
}
这篇关于在ListFragment内的适配器上调用notifyDataSetChanged()会导致NullPointerException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!