使用Intents传递数据会导致app android崩溃

时间:2014-04-08 14:14:50

标签: android android-intent crash

当我尝试使用意图传递一些数据时,为什么我的应用程序崩溃?

 case 0:
             Intent intent=new Intent(context, RMpro.class);
             intent.putExtra("NAME", "Projectile!");
             intent.putExtra("PATH", R.raw.s14projectil);
             intent.putExtra("FILE", "Projectile.mp3");
             ((Activity)context).startActivity(intent);

             break;

在RMpro活动中:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ringtone);

  Intent i=this.getIntent();

       final  String C1 = getIntent().getStringExtra("PATH").trim();
       final  String A1 = getIntent().getStringExtra("NAME").trim();
       final  String B1 = getIntent().getStringExtra("FILE").trim();

为什么会这样? 我的Logcat:

04-08 16:07:34.811: E/AndroidRuntime(1174): FATAL EXCEPTION: main
04-08 16:07:34.811: E/AndroidRuntime(1174): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mostpopularalarmringtones/com.mostpopularalarmringtones.RMpro}: java.lang.NullPointerException
04-08 16:07:34.811: E/AndroidRuntime(1174):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2266)
04-08 16:07:34.811: E/AndroidRuntime(1174):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2316)
04-08 16:07:34.811: E/AndroidRuntime(1174):     at android.app.ActivityThread.access$600(ActivityThread.java:150)
04-08 16:07:34.811: E/AndroidRuntime(1174):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1298)
04-08 16:07:34.811: E/AndroidRuntime(1174):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-08 16:07:34.811: E/AndroidRuntime(1174):     at android.os.Looper.loop(Looper.java:213)
04-08 16:07:34.811: E/AndroidRuntime(1174):     at android.app.ActivityThread.main(ActivityThread.java:5225)
04-08 16:07:34.811: E/AndroidRuntime(1174):     at java.lang.reflect.Method.invokeNative(Native Method)
04-08 16:07:34.811: E/AndroidRuntime(1174):     at java.lang.reflect.Method.invoke(Method.java:525)
04-08 16:07:34.811: E/AndroidRuntime(1174):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741)
04-08 16:07:34.811: E/AndroidRuntime(1174):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
04-08 16:07:34.811: E/AndroidRuntime(1174):     at dalvik.system.NativeStart.main(Native Method)
04-08 16:07:34.811: E/AndroidRuntime(1174): Caused by: java.lang.NullPointerException
04-08 16:07:34.811: E/AndroidRuntime(1174):     at com.mostpopularalarmringtones.RMpro.onCreate(RMpro.java:37)
04-08 16:07:34.811: E/AndroidRuntime(1174):     at android.app.Activity.performCreate(Activity.java:5133)
04-08 16:07:34.811: E/AndroidRuntime(1174):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-08 16:07:34.811: E/AndroidRuntime(1174):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2230)
04-08 16:07:34.811: E/AndroidRuntime(1174):     ... 11 more

我在这里做错了什么?为什么我的应用程序崩溃?

3 个答案:

答案 0 :(得分:1)

您应始终检查getIntent()的结果是否为null

Intent i = getIntent();
if (i != null){
    final  String C1 = i.getStringExtra("PATH").trim();
    final  String A1 = i.getStringExtra("NAME").trim();
    final  String B1 = i.getStringExtra("FILE").trim();
    ....
}

此外,当您将数据放入Intent并稍后使用getStringExtra检索数据时,您应该确保放置StringR.raw.s14projectil的值始终为int。如果s14projectil是包含数据的文件,则应首先打开InputStream来阅读它:

InputStream raw = getResources().openRawResource(R.raw.s14projectil);

然后才将内容传递给意图。

答案 1 :(得分:0)

您将int加入了额外内容(R.raw.s14projectil的值为int):

intent.putExtra("PATH", R.raw.s14projectil);

然后尝试检索字符串:

final String C1 = i.getStringExtra("PATH").trim();

这就是你得到NullPointerException

的原因

答案 2 :(得分:0)

使用此代码可能会对您有所帮助。

Case 0:
Intent intent=new Intent(context, RMpro.class);
Bundle extra = new Bundle();
extra.putString("NAME", "Projectile!");
extra.putString("PATH", R.raw.s14projectil);
extra.putString("FILE", "Projectile.mp3");
intent.putExtras(extra);
((Activity)context).startActivity(intent);

在RMpro活动中:

Intent Rmintent= getIntent();
Bundle extras = Rmintent.getExtras();
final  String C1 = extras.getString("PATH").trim();
final  String A1 = extras.getString("NAME").trim();
final  String B1 = extras.getString("FILE").trim();