为什么我的应用程序强制关闭在android中使用getarguments()。getstring(key)?

时间:2014-05-07 09:52:44

标签: android android-fragments nullpointerexception

我有以下片段。 1. addtaskfragment extends Fragment 2. mycontacts extends Fragment 我目前正在做以下事情。 我在addtaskfragment中有一些文本字段和按钮。单击按钮后,我可以转到mycontacts片段。我将mycontacts中的值传递给addtaskfragment。问题是我的应用程序正在关闭力量。我的代码如下。请一步一步告诉我该做什么?

addtaskfragment

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.addtasknew, container,
            false);
       final String strtext=getArguments().getString("k");
 addcontacts.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

     Fragment fr;
      fr=new mycontacts();
      FragmentManager fm=getFragmentManager();
      android.app.FragmentTransaction ft=fm.beginTransaction();
      Bundle args = new Bundle();
      args.putString("temp","1");
    //  args.putString("taskname",datepick.getText().toString());
    //  Log.i("temp from long click",""+temp);
      fr.setArguments(args);
      ft.replace(R.id.content_frame, fr);

               ft.commit();
}
  });
     }

mycontacts

          Fragment fr;
          fr=new addtaskfragment();
          FragmentManager fm=getFragmentManager();
          android.app.FragmentTransaction ft=fm.beginTransaction();
          Bundle args = new Bundle();
          args.putString("CID", cid);
          args.putString("k", cid);

          Log.i("cid",""+args);
          fr.setArguments(args);
          ft.replace(R.id.content_frame, fr);

                   ft.commit();

错误日志

  E/AndroidRuntime(4507): FATAL EXCEPTION: main
  E/AndroidRuntime(4507): java.lang.NullPointerException
  E/AndroidRuntime(4507):   at com.example.taskmanager.addtaskfragment.onCreateView(addtaskfragment.java:62)
  E/AndroidRuntime(4507):   at android.app.Fragment.performCreateView(Fragment.java:1695)
  E/AndroidRuntime(4507):   at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:885)
  E/AndroidRuntime(4507):   at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
  E/AndroidRuntime(4507):   at android.app.BackStackRecord.run(BackStackRecord.java:682)
  E/AndroidRuntime(4507):   at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
  E/AndroidRuntime(4507):   at android.app.FragmentManagerImpl$1.run(FragmentManager.java:441)
  E/AndroidRuntime(4507):   at android.os.Handler.handleCallback(Handler.java:725)
  E/AndroidRuntime(4507):   at android.os.Handler.dispatchMessage(Handler.java:92)
  E/AndroidRuntime(4507):   at android.os.Looper.loop(Looper.java:137)
  E/AndroidRuntime(4507):   at android.app.ActivityThread.main(ActivityThread.java:5041)
  E/AndroidRuntime(4507):   at java.lang.reflect.Method.invokeNative(Native Method)
  E/AndroidRuntime(4507):   at java.lang.reflect.Method.invoke(Method.java:511)
  E/AndroidRuntime(4507):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
  E/AndroidRuntime(4507):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
  E/AndroidRuntime(4507):   at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:2)

确保您的参数不为空

答案 1 :(得分:0)

您尝试调用addcontacts的{​​{1}}是setOnClickListener()