当我使用putExtra发送数据并使用getStringExtra接收数据时,应用程序崩溃?

时间:2016-01-19 08:52:55

标签: android android-intent android-activity

在我的应用程序中,我想使用intent类将一些字符串数据从一个类传递到另一个类。我使用以下putExtra将数据发送到另一个名为BusDetail的类。

        @Override
        public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id)
        {

            if(groupPosition==0)
            {
                if(childPosition == 0)
                {
                    Intent child0Intent = new Intent(getBaseContext(), BusDetail.class);
                    child0Intent.putExtra("ROOT",root_name[0]);
                    startActivity(child0Intent);
                }

            }

            if(groupPosition==1)
            {
                if(childPosition == 0)
                {
                    Intent child0Intent = new Intent(getBaseContext(), BusDetail.class);
                    child0Intent.putExtra("ROOT",root_name[0]);
                    startActivity(child0Intent);
                }

            }
            return false;
        }
    });
}

以下是BusDetail类,我使用getStringExtra从上面的类中收集数据。但是当我上课到下课时,我的应用程序崩溃了

final Intent intent=getIntent();
//passedArg=in

final String id = intent.getStringExtra("ROOT");

不使用putExtragetStringExtra,我的应用就可以了。

这是我的logcat

01-19 15:22:23.078  22686-22686/com.example.kuet.traveldirectory E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.kuet.traveldirectory, PID: 22686
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.kuet.traveldirectory/com.example.kuet.traveldirectory.BusDetail}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2236)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
        at android.app.ActivityThread.access$800(ActivityThread.java:151)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5257)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference
        at com.example.kuet.traveldirectory.BusDetail.<init>(BusDetail.java:32)
        at java.lang.reflect.Constructor.newInstance(Native Method)
        at java.lang.Class.newInstance(Class.java:1606)
        at android.app.Instrumentation.newActivity(Instrumentation.java:1066)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2226)

2 个答案:

答案 0 :(得分:0)

在您当前的活动中,创建一个新的意图:

Intent child0Intent = new Intent(getBaseContext(), BusDetail.class);
               child0Intent.putExtra("ROOT",root_name[0]);
                startActivity(child0Intent);

然后在新的Activity中,检索这些值:

Bundle extras = getIntent().getExtras();
if (extras != null) {
    String value = extras.getString("ROOT");
}

使用此技术将变量从一个Activity传递到另一个Activity。

答案 1 :(得分:-1)

你应该尝试,

final String id = intent.getExtras().getString("ROOT");