I am trying to call a fragment method from an activity but I am getting a null pointer exception when setting the adapter context with getActivity()
in that method:
Fragment Method
public void fragmentMethod() {
String[] names = new String[]{"name1", "name2", "name3"};
String[] values = new String[]{value1, value2, value3};
adapter = new MyArrayAdapter(getActivity(), names, values);
setListAdapter(adapter);
}
Adapter
public class MyArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] names;
private final String[] values;
public MyArrayAdapter (Context context, String[] names, String[] values) {
super(context, R.layout.rowlayout, values);
this.context = context;
this.names = names;
this.values = values;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
...
}
}
Call to Fragment Method from Activity
public void activityMethod() {
FragmentName test= new FragmentName();
test.fragmentMethod();
}
I have tried many different things and I'm at a loss here...if I am fundamentally wrong in what I am trying to do, please let me know as I am a novice. Thank you!
Edit: Stack Trace
( Inventory
is the Fragment class)
This is after clicking an imageview which calls activityMethod()
06-04 19:55:17.976 1147-1147/company.com.myapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at android.widget.ArrayAdapter.init(ArrayAdapter.java:310)
at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:128)
at company.com.myapp.MyArrayAdapter.<init>(MyArrayAdapter.java:18)
at company.com.myapp.Inventory.createList(Inventory.java:105)
at company.com.myapp.MainActivity.newDay(MainActivity.java:177)
at company.com.myapp.MainActivity$2.onClick(MainActivity.java:91)
at android.view.View.performClick(View.java:4240)
at android.view.View$PerformClick.run(View.java:17721)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
if I am fundamentally wrong in what I am trying to do, please let me know as I am a novice
You cannot call getActivity()
and get a response until the fragment is attached to an activity. In the case of a dynamic fragment — one you are creating directly via the constructor — this will be sometime after you commit()
a FragmentTransaction
that will put this fragment in a FragmentManager
.
Moreover, there is no point in setting up your adapter until the ListView
is ready.
So, simply move your fragmentMethod()
code into an onViewCreated()
method on your fragment. This will be called at a point in time when both the fragment is attached to the activity and the ListView
is ready to receive your adapter.
From the code above, your fragment didn't associate to any activity. So getActivity()
call will return null until you attach your fragment to activity.
private void activityMethod() {
MyFragment fragment = new MyFragment();
getSupportFragmentManager().beginTransaction()
.add(fragment, "myfragment").commit();
fragment.fragmentMethod();
}
在onActivityCreated()
或onAttach()
内onAttach()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.