空指针异常将活动中的数据包数据发送到片段

时间:2014-07-13 04:15:47

标签: java android android-fragments nullpointerexception

我正在尝试将活动类中的数据包数据发送到片段类,但我一直在收到NPE错误。我已经对Stackoverflow进行了一些研究并尝试了一些不同的东西,但似乎都没有。

我有另一个活动类将意图发送到MainActivity类。因此,当MainActivity.java发送到MyFragment类时,它绝对不是null。

请帮忙!谢谢! :d

MainActivity.java

public class MainActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      requestWindowFeature(Window.FEATURE_NO_TITLE);
      setContentView(R.layout.activity_main);

      Bundle extras = getIntent().getExtras();
      if (extras != null) {
        Bundle bundle = new Bundle();
        String value = extras.getString("TEST36");
        bundle.putString("TEST36", value);
        MyFragment fragInfo = new MyFragment();
        fragInfo.setArguments(bundle);
      }
  }
}

MyFragment.java

public class MyFragment extends Fragment{
   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup parent,
        Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.fragment_xml, parent, false);
   }
   @Override
   public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    Bundle bundle = getArguments();                                             
    String myMode = bundle.getString("TEST36");
   }
}

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_xml"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:name="com.example.test.MyFragment"> 
</fragment>

fragment_xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/TESTINGTV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="26dp"
        android:layout_marginTop="21dp"
        android:layout_marginBottom="20dp"
        android:textSize="@dimen/text_size" />

</RelativeLayout>

logcat的

07-12 01:01:59.149: E/AndroidRuntime(2950): FATAL EXCEPTION: main
07-12 01:01:59.149: E/AndroidRuntime(2950): Process: com.example.test, PID: 2950
07-12 01:01:59.149: E/AndroidRuntime(2950): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.MainActivity}: java.lang.NullPointerException
07-12 01:01:59.149: E/AndroidRuntime(2950):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
07-12 01:01:59.149: E/AndroidRuntime(2950):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
07-12 01:01:59.149: E/AndroidRuntime(2950):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
07-12 01:01:59.149: E/AndroidRuntime(2950):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
07-12 01:01:59.149: E/AndroidRuntime(2950):     at android.os.Handler.dispatchMessage(Handler.java:102)
07-12 01:01:59.149: E/AndroidRuntime(2950):     at android.os.Looper.loop(Looper.java:136)
07-12 01:01:59.149: E/AndroidRuntime(2950):     at android.app.ActivityThread.main(ActivityThread.java:5017)
07-12 01:01:59.149: E/AndroidRuntime(2950):     at java.lang.reflect.Method.invokeNative(Native Method)
07-12 01:01:59.149: E/AndroidRuntime(2950):     at java.lang.reflect.Method.invoke(Method.java:515)
07-12 01:01:59.149: E/AndroidRuntime(2950):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-12 01:01:59.149: E/AndroidRuntime(2950):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-12 01:01:59.149: E/AndroidRuntime(2950):     at dalvik.system.NativeStart.main(Native Method)
07-12 01:01:59.149: E/AndroidRuntime(2950): Caused by: java.lang.NullPointerException
07-12 01:01:59.149: E/AndroidRuntime(2950):     at com.example.Fragment.onActivityCreated(MyFragment.java:1098)
07-12 01:01:59.149: E/AndroidRuntime(2950):     at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1508)
07-12 01:01:59.149: E/AndroidRuntime(2950):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
07-12 01:01:59.149: E/AndroidRuntime(2950):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
07-12 01:01:59.149: E/AndroidRuntime(2950):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1086)
07-12 01:01:59.149: E/AndroidRuntime(2950):     at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1884)
07-12 01:01:59.149: E/AndroidRuntime(2950):     at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:566)
07-12 01:01:59.149: E/AndroidRuntime(2950):     at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
07-12 01:01:59.149: E/AndroidRuntime(2950):     at android.app.Activity.performStart(Activity.java:5241)
07-12 01:01:59.149: E/AndroidRuntime(2950):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2168)
07-12 01:01:59.149: E/AndroidRuntime(2950):     ... 11 more

2 个答案:

答案 0 :(得分:0)

行中的问题

MyFragment fragInfo = new MyFragment();

您正在创建一个新的片段实例,并将数据传递给它,但该片段不是UI的一部分。相反,您可能希望使用

在UI中找到片段实例
findFragmentById(R.id.my_fragment_id)

答案 1 :(得分:0)

MyFragment fragInfo = new MyFragment();

之后添加以下代码
fragInfo.setArguments(bundle);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.my_fragment_id, fragInfo);
transaction.commit();

尝试获取onCreateView中的数据

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
       Bundle savedInstanceState)
{
    String myValue = this.getArguments().getString("TEST36");