vir*_*hum 4 parameters android fragment android-fragmentactivity
当我试图将一个参数从FragmentActivity传递给Fragment时,它在Fragment的getArguments()中给出了空指针异常.
这是我的FragmentActivity Code
public class IndexChartActivity extends FragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.index_chart);
IndexFragmentActivity indexFragment =
(IndexFragmentActivity)getSupportFragmentManager().findFragmentById(R.id.index_fragment);
indexFragment.newInstance("ASPI");
}
}
Run Code Online (Sandbox Code Playgroud)
这是index_chart.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:orientation="vertical" >
<fragment
android:id="@+id/header_fragment"
android:name="com.lk.ignitionit.cse.util.HeaderFragmentActivity"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<fragment
android:id="@+id/index_fragment"
android:name="com.lk.ignitionit.cse.util.IndexFragmentActivity"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<fragment
android:name="com.lk.ignitionit.cse.util.ChartFragmentActivity"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
这是碎片
public class IndexFragmentActivity extends Fragment {
protected ImageView ivASPI;
protected ImageView ivMPI;
protected ImageView ivSP;
protected TextView tvMain;
protected TextView tvTop;
protected TextView tvBottom;
String response = null;
String result = null;
String [] resultArr = null;
Bundle b = new Bundle();
String indexType = null;
int layout;
IndexFragmentActivity f = null;
public IndexFragmentActivity newInstance(String index) {
f = new IndexFragmentActivity();
Bundle args = new Bundle();
args.putString("indextype", index);
f.setArguments(args);
return f;
}
public String getSelectedIndex() {
return f.getArguments().getString("indextype");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if(getSelectedIndex().equals("ASPI")){
layout = R.layout.aspi_header;
}else if(getSelectedIndex().equals("MPI")){
layout = R.layout.mpi_header;
}else{
layout = R.layout.sp_header;
}
View view = inflater.inflate(layout, container, false);
tvMain = (TextView) view.findViewById(R.id.tv_main);
tvTop = (TextView) view.findViewById(R.id.tv_top);
tvBottom = (TextView) view.findViewById(R.id.tv_bottom);
new ServiceAccess().execute("");
tvMain.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
b.putString("type", "S&P SL20");
Activity activity = getActivity();
Intent intent = new Intent(activity, IndexChartActivity.class);
intent.putExtras(b);
startActivity(intent);
}
});
return view;
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的错误
Caused by: java.lang.NullPointerException
at com.lk.ignitionit.cse.util.IndexFragmentActivity.getSelectedIndex(IndexFragmentActivity.java:69)
Run Code Online (Sandbox Code Playgroud)
我提到了很多与此相关的stackoverflow问题,并尝试了http://developer.android.com/guide/components/fragments.html给出的示例代码.但仍然没有运气
真的很感激任何反馈,因为这似乎是一个非常基本的问题,我无法弄清楚..
提前致谢
Com*_*are 18
摆脱Activity所有不继承的类的名称Activity.例如,
IndexFragmentActivity不是Activity.
在onCreate()您的实际活动中,您正在调用findFragmentById()检索片段,然后调用newInstance()该片段.但是,第一次onCreate()调用时片段不会存在,并且你将在这里失败NullPointerException.请正确处理此案例.
newInstanceJava中的方法名称通常与工厂模式相关联,其中使用newInstance()的static方法代替公共构造函数来创建某个类的实例.你的newInstance()方法不是static.对于那些来维护代码的人来说,这会引起混淆.
你打电话newInstance()的onCreate(),创建新的片段实例,然后把它扔掉,这是浪费时间.
因此,假设您的片段的原始实例(无论它来自何处)没有Bundle设置参数,这将解释您的NullPointerExceptionin getSelectedIndex().
当我试图将一个参数从FragmentActivity传递给Fragment时
要将参数传递给新创建的片段,使用static newInstance()方法和参数Bundle来创建新片段是完全合理的.
要将参数传递给已存在的片段,只需在该片段上调用一些setter方法即可.
| 归档时间: |
|
| 查看次数: |
15593 次 |
| 最近记录: |