简体   繁体   English

使用.getInt(Android)时NullPointerException和应用程序崩溃

[英]NullPointerException and app crashes when using .getInt (Android)

I'm using an intent and getIntent on my Android app but it crashes in the emulator. 我在我的Android应用程序上使用了intent和getIntent,但它在模拟器中崩溃了。 Logcat indicates that my problem it's a NullPointerException but since yesterday I just can't find the solution for this problem. Logcat表明我的问题是NullPointerException但是从昨天起我就找不到解决这个问题的方法了。

Here is my Logcat: 这是我的Logcat:

10-05 20:27:12.053: E/AndroidRuntime(246): Uncaught handler: thread main exiting due to uncaught exception
10-05 20:27:12.063: E/AndroidRuntime(246): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.Tonos/com.example.Tonos.TonosSet}: java.lang.NullPointerException
10-05 20:27:12.063: E/AndroidRuntime(246):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
10-05 20:27:12.063: E/AndroidRuntime(246):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
10-05 20:27:12.063: E/AndroidRuntime(246):  at android.app.ActivityThread.access$2200(ActivityThread.java:119)
10-05 20:27:12.063: E/AndroidRuntime(246):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
10-05 20:27:12.063: E/AndroidRuntime(246):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-05 20:27:12.063: E/AndroidRuntime(246):  at android.os.Looper.loop(Looper.java:123)
10-05 20:27:12.063: E/AndroidRuntime(246):  at android.app.ActivityThread.main(ActivityThread.java:4363)
10-05 20:27:12.063: E/AndroidRuntime(246):  at java.lang.reflect.Method.invokeNative(Native Method)
10-05 20:27:12.063: E/AndroidRuntime(246):  at java.lang.reflect.Method.invoke(Method.java:521)
10-05 20:27:12.063: E/AndroidRuntime(246):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
10-05 20:27:12.063: E/AndroidRuntime(246):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
10-05 20:27:12.063: E/AndroidRuntime(246):  at dalvik.system.NativeStart.main(Native Method)
10-05 20:27:12.063: E/AndroidRuntime(246): Caused by: java.lang.NullPointerException
10-05 20:27:12.063: E/AndroidRuntime(246):  at com.example.Tonos.TonosSet.onCreate(TonosSet.java:21)
10-05 20:27:12.063: E/AndroidRuntime(246):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-05 20:27:12.063: E/AndroidRuntime(246):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
10-05 20:27:12.063: E/AndroidRuntime(246):  ... 11 more

This is my "TonosSet.java": 这是我的“TonosSet.java”:

package com.example.Tonos;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;


public class TonosSet extends Activity {
/**
 * @see android.app.Activity#onCreate(Bundle)
 */
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.tonos_set);

    Intent i = getIntent();
    int position = i.getExtras().getInt("id");

    ImageView imageView = (ImageView) findViewById(R.id.full_image_view2);
    TextView textView1 = (TextView)findViewById(R.id.namesong);
    TextView textView2 = (TextView)findViewById(R.id.artist);

    switch (position){
    case 0:        
        imageView.setImageResource(R.drawable.pico);

        textView1.setText("Tone1");
        textView2.setText("Someone");        
    break;

    case 1:        
        imageView.setImageResource(R.drawable.pict);

        textView1.setText("Tone2");
        textView2.setText("Someone");           
    break;

    }


}
}

And here is the part of code that creates the Intent "i" (Tonos.java): 以下是创建Intent“i”(Tonos.java)的代码部分:

....            
maListViewPerso.setAdapter(mSchedule);

    maListViewPerso.setOnItemClickListener(new OnItemClickListener() {
            @Override
        @SuppressWarnings("unchecked")
        public void onItemClick(AdapterView<?> a, View v, int position, long id){
                HashMap<String, String> map = (HashMap<String, String>) maListViewPerso.getItemAtPosition(position);

                Toast.makeText(Tonos.this, "" + position, Toast.LENGTH_SHORT).show();

                Intent tonosset = new Intent(Tonos.this, TonosSet.class);
                startActivity(tonosset);

                Intent i = new Intent(getApplicationContext(), TonosSet.class);

                i.putExtra("id", position);
                startActivity(i);

            }


    });

}
}

Thanks a lot guys! 非常感谢! =) =)

 // First time you start TonosSet activity (without extra)
 // If you want to start TonosSet activity only once, comment two following line
 Intent tonosset = new Intent(Tonos.this, TonosSet.class);
 startActivity(tonosset);

 // Second time you start TonosSet activity (with extra this time)
 Intent i = new Intent(getApplicationContext(), TonosSet.class);
 i.putExtra("id", position);
 startActivity(i);

You start TonosSet activity twice. 你开始两次TonosSet活动。 First time you launch it, the intent doesn't have extra. 第一次启动时,意图没有额外的意图。 To resolve your problem check your intent have extra. 要解决您的问题,请检查您的意图。

Intent i = getIntent();
int position = -1; // init with default value
if(i.getExtras() != null)
    position = i.getExtras().getInt("id");
            Intent tonosset = new Intent(Tonos.this, TonosSet.class); 
            startActivity(tonosset); 

            Intent i = new Intent(getApplicationContext(), TonosSet.class); 

            i.putExtra("id", position); 
            startActivity(i); 

You are creating two intents: one has an extra and the other doesn't. 您正在创建两个意图:一个有额外的,另一个没有。 The NPE occurs when you try to get an extra from the activity that doesn't have one. 当您尝试从没有活动的活动中获取额外费用时,就会发生NPE。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM