尝试在片段中使用 Bundle 调用虚拟方法 java.util.ArrayList
Attempt to invoke virtual method java.util.ArrayList using Bundle in fragment
嗨,我正在尝试将 ArrayList throw Bundle 发送到 Fragment,我遇到了这个问题(问题是在获取 ArrayList 时引起的)并且我在发送它之前已经测试过它不是 null ,
我已经用
发送了
arg.putStringArrayList("lp", li);
并通过
接收
ArrayList<String> lil = getArguments().getStringArrayList("lp");
这是 StackTrace:
04-18 20:43:55.153 17577-17577/com.pfe.elmokhtar.domotique W/System.err﹕ java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.ArrayList android.os.Bundle.getStringArrayList(java.lang.String)' on a null object reference
04-18 20:43:55.153 17577-17577/com.pfe.elmokhtar.domotique W/System.err﹕ at com.pfe.elmokhtar.domotique.DetailFragment.onCreateView(DetailFragment.java:31)
04-18 20:43:55.158 17577-17577/com.pfe.elmokhtar.domotique W/System.err﹕ at android.app.Fragment.performCreateView(Fragment.java:2053)
04-18 20:43:55.158 17577-17577/com.pfe.elmokhtar.domotique W/System.err﹕ at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:894)
04-18 20:43:55.158 17577-17577/com.pfe.elmokhtar.domotique W/System.err﹕ at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
04-18 20:43:55.158 17577-17577/com.pfe.elmokhtar.domotique W/System.err﹕ at android.app.BackStackRecord.run(BackStackRecord.java:833)
04-18 20:43:55.158 17577-17577/com.pfe.elmokhtar.domotique W/System.err﹕ at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1452)
04-18 20:43:55.160 17577-17577/com.pfe.elmokhtar.domotique W/System.err﹕ at android.app.FragmentManagerImpl.run(FragmentManager.java:447)
04-18 20:43:55.160 17577-17577/com.pfe.elmokhtar.domotique W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:739)
04-18 20:43:55.160 17577-17577/com.pfe.elmokhtar.domotique W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:95)
04-18 20:43:55.160 17577-17577/com.pfe.elmokhtar.domotique W/System.err﹕ at android.os.Looper.loop(Looper.java:135)
04-18 20:43:55.160 17577-17577/com.pfe.elmokhtar.domotique W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5254)
04-18 20:43:55.160 17577-17577/com.pfe.elmokhtar.domotique W/System.err﹕ at java.lang.reflect.Method.invoke(Native Method)
04-18 20:43:55.160 17577-17577/com.pfe.elmokhtar.domotique W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:372)
04-18 20:43:55.160 17577-17577/com.pfe.elmokhtar.domotique W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
04-18 20:43:55.160 17577-17577/com.pfe.elmokhtar.domotique W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
这就是我实例化片段的方式
public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
dLayout.closeDrawers();
Bundle arg = new Bundle();
arg.putString("Menu", menu[position]);
Fragment detail = new DetailFragment();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, detail).commit();
if (position == 0) {
/*
Pieces
*/
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
invokeWS(); //WebService Invocation it run fine
arg.putStringArrayList("lp", li);
if(li.isEmpty())
System.out.println("null Catched !");
}
/*
Pieces
*/
if (position == 5) {
Intent intent = new Intent(Home.this, CheckLoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
}
});
这是 Class 细节片段
package com.pfe.elmokhtar.domotique;
/**
* Created by elmokhtar on 3/11/15.
*/
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
public class DetailFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle args) {
View view = inflater.inflate(R.layout.detailfragment, container, false);
// TextView text= (TextView) view.findViewById(R.id.detail);
ListView list = (ListView) view.findViewById(R.id.list);
Bundle test = getArguments();
// String menu = getArguments().getString("Menu");
try {
ArrayList < String > lil = test.getStringArrayList("lp");
System.out.println("done!");
ArrayAdapter < String > adapter;
adapter = new ArrayAdapter < String > (this.getActivity(), android.R.layout.simple_list_item_1, lil);
list.setAdapter(adapter);
} catch (Exception e) {
e.printStackTrace();
}
//text.setText("test");
return view;
}
}
Fragment detail = new DetailFragment();
Bundle arg = new Bundle();
arg.putStringArrayList("lp", li);
detail.setArguments(arg);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, detail).commit();
按照我在评论中的建议将参数传递给片段:
public void onItemClick(AdapterView <? > arg0, View v, int position, long id) {
dLayout.closeDrawers();
Bundle arg = new Bundle();
arg.putString("Menu", menu[position]);
arg.putStringArrayList("lp", li);
Fragment detail = new DetailFragment();
detail.setArguments(arg);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, detail).commit();
//...
}
创建片段实例并将值传递给它的首选方法
Activity : 像这样创建 Fragment 实例
Fragment detail=DetailFragment.newInstace(li);
fragmentManager.beginTransaction().replace(R.id.content_frame, detail).commit();
Fragment : 始终为片段
创建一个 默认构造函数
All subclasses of Fragment must include a public no-argument constructor.
public class DetailFragment extends Fragment {
public DetailFragment() {
}
public static Fragment newInstace(ArrayList<String> items) {
Fragment fragment = new DetailFragment();
Bundle bundle = new Bundle();
bundle.putStringArrayList("key", items);
fragment.setArguments(bundle);
return fragment;
}
//your stuff
}
嗨,我正在尝试将 ArrayList throw Bundle 发送到 Fragment,我遇到了这个问题(问题是在获取 ArrayList 时引起的)并且我在发送它之前已经测试过它不是 null , 我已经用
发送了arg.putStringArrayList("lp", li);
并通过
接收ArrayList<String> lil = getArguments().getStringArrayList("lp");
这是 StackTrace:
04-18 20:43:55.153 17577-17577/com.pfe.elmokhtar.domotique W/System.err﹕ java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.ArrayList android.os.Bundle.getStringArrayList(java.lang.String)' on a null object reference
04-18 20:43:55.153 17577-17577/com.pfe.elmokhtar.domotique W/System.err﹕ at com.pfe.elmokhtar.domotique.DetailFragment.onCreateView(DetailFragment.java:31)
04-18 20:43:55.158 17577-17577/com.pfe.elmokhtar.domotique W/System.err﹕ at android.app.Fragment.performCreateView(Fragment.java:2053)
04-18 20:43:55.158 17577-17577/com.pfe.elmokhtar.domotique W/System.err﹕ at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:894)
04-18 20:43:55.158 17577-17577/com.pfe.elmokhtar.domotique W/System.err﹕ at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
04-18 20:43:55.158 17577-17577/com.pfe.elmokhtar.domotique W/System.err﹕ at android.app.BackStackRecord.run(BackStackRecord.java:833)
04-18 20:43:55.158 17577-17577/com.pfe.elmokhtar.domotique W/System.err﹕ at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1452)
04-18 20:43:55.160 17577-17577/com.pfe.elmokhtar.domotique W/System.err﹕ at android.app.FragmentManagerImpl.run(FragmentManager.java:447)
04-18 20:43:55.160 17577-17577/com.pfe.elmokhtar.domotique W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:739)
04-18 20:43:55.160 17577-17577/com.pfe.elmokhtar.domotique W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:95)
04-18 20:43:55.160 17577-17577/com.pfe.elmokhtar.domotique W/System.err﹕ at android.os.Looper.loop(Looper.java:135)
04-18 20:43:55.160 17577-17577/com.pfe.elmokhtar.domotique W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5254)
04-18 20:43:55.160 17577-17577/com.pfe.elmokhtar.domotique W/System.err﹕ at java.lang.reflect.Method.invoke(Native Method)
04-18 20:43:55.160 17577-17577/com.pfe.elmokhtar.domotique W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:372)
04-18 20:43:55.160 17577-17577/com.pfe.elmokhtar.domotique W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
04-18 20:43:55.160 17577-17577/com.pfe.elmokhtar.domotique W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
这就是我实例化片段的方式
public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
dLayout.closeDrawers();
Bundle arg = new Bundle();
arg.putString("Menu", menu[position]);
Fragment detail = new DetailFragment();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, detail).commit();
if (position == 0) {
/*
Pieces
*/
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
invokeWS(); //WebService Invocation it run fine
arg.putStringArrayList("lp", li);
if(li.isEmpty())
System.out.println("null Catched !");
}
/*
Pieces
*/
if (position == 5) {
Intent intent = new Intent(Home.this, CheckLoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
}
});
这是 Class 细节片段
package com.pfe.elmokhtar.domotique;
/**
* Created by elmokhtar on 3/11/15.
*/
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
public class DetailFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle args) {
View view = inflater.inflate(R.layout.detailfragment, container, false);
// TextView text= (TextView) view.findViewById(R.id.detail);
ListView list = (ListView) view.findViewById(R.id.list);
Bundle test = getArguments();
// String menu = getArguments().getString("Menu");
try {
ArrayList < String > lil = test.getStringArrayList("lp");
System.out.println("done!");
ArrayAdapter < String > adapter;
adapter = new ArrayAdapter < String > (this.getActivity(), android.R.layout.simple_list_item_1, lil);
list.setAdapter(adapter);
} catch (Exception e) {
e.printStackTrace();
}
//text.setText("test");
return view;
}
}
Fragment detail = new DetailFragment();
Bundle arg = new Bundle();
arg.putStringArrayList("lp", li);
detail.setArguments(arg);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, detail).commit();
按照我在评论中的建议将参数传递给片段:
public void onItemClick(AdapterView <? > arg0, View v, int position, long id) {
dLayout.closeDrawers();
Bundle arg = new Bundle();
arg.putString("Menu", menu[position]);
arg.putStringArrayList("lp", li);
Fragment detail = new DetailFragment();
detail.setArguments(arg);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, detail).commit();
//...
}
创建片段实例并将值传递给它的首选方法
Activity : 像这样创建 Fragment 实例
Fragment detail=DetailFragment.newInstace(li);
fragmentManager.beginTransaction().replace(R.id.content_frame, detail).commit();
Fragment : 始终为片段
创建一个 默认构造函数All subclasses of Fragment must include a public no-argument constructor.
public class DetailFragment extends Fragment {
public DetailFragment() {
}
public static Fragment newInstace(ArrayList<String> items) {
Fragment fragment = new DetailFragment();
Bundle bundle = new Bundle();
bundle.putStringArrayList("key", items);
fragment.setArguments(bundle);
return fragment;
}
//your stuff
}