我在使用此片段事务时遇到问题。我尝试使用toast消息代替editData函数,它工作正常。有谁知道出了什么问题?
以下是代码:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.edit:
editData();
break;
}
return super.onOptionsItemSelected(item);
}
public void editData() {
EditFragment fragment = new EditFragment();
Bundle args = new Bundle();
TextView tv = (TextView) getView().findViewById(R.id.groupName);
String groupName = tv.getText().toString();
args.putString(EditFragment.ARG_GROUPNAME, groupName);
FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_info, fragment);
transaction.addToBackStack(null);
transaction.commit();
}
这是logcat:
01-03 00:25:11.565: E/AndroidRuntime(21353): FATAL EXCEPTION: main
01-03 00:25:11.565: E/AndroidRuntime(21353): Process: com.john.oper, PID: 21353
01-03 00:25:11.565: E/AndroidRuntime(21353): java.lang.NullPointerException
01-03 00:25:11.565: E/AndroidRuntime(21353): at com.john.oper.EditFragment.onCreateView(EditFragment.java:29)
01-03 00:25:11.565: E/AndroidRuntime(21353): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478)
01-03 00:25:11.565: E/AndroidRuntime(21353): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
01-03 00:25:11.565: E/AndroidRuntime(21353): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
01-03 00:25:11.565: E/AndroidRuntime(21353): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
01-03 00:25:11.565: E/AndroidRuntime(21353): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
01-03 00:25:11.565: E/AndroidRuntime(21353): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
01-03 00:25:11.565: E/AndroidRuntime(21353): at android.os.Handler.handleCallback(Handler.java:733)
01-03 00:25:11.565: E/AndroidRuntime(21353): at android.os.Handler.dispatchMessage(Handler.java:95)
01-03 00:25:11.565: E/AndroidRuntime(21353): at android.os.Looper.loop(Looper.java:136)
01-03 00:25:11.565: E/AndroidRuntime(21353): at android.app.ActivityThread.main(ActivityThread.java:5017)
01-03 00:25:11.565: E/AndroidRuntime(21353): at java.lang.reflect.Method.invokeNative(Native Method)
01-03 00:25:11.565: E/AndroidRuntime(21353): at java.lang.reflect.Method.invoke(Method.java:515)
01-03 00:25:11.565: E/AndroidRuntime(21353): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
01-03 00:25:11.565: E/AndroidRuntime(21353): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
01-03 00:25:11.565: E/AndroidRuntime(21353): at dalvik.system.NativeStart.main(Native Method)
这是EditFragment的onCreateView:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
Bundle data = getArguments();
EditText et = (EditText) getView().findViewById(R.id.groupName);
et.setText(data.getString(ARG_GROUPNAME));
return inflater.inflate(R.layout.fragment_edit, container, false);
}
答案 0 :(得分:0)
您没有将参数设置为片段。
fragment.setArguments(args);
当你尝试在片段参数上做一个get字符串时,你就崩溃了。
编辑:此外,您正在尝试访问getView()
中的片段视图(onCreateView()
),这是创建视图的位置,因此它将为null。您应该将该逻辑推迟到onViewCreated()
。