1

我在我的 Android 应用程序上使用了一个意图和 getIntent,但它在模拟器中崩溃了。Logcat 表明我的问题是 NullPointerException 但从昨天开始我就是找不到解决这个问题的方法。

这是我的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

这是我的“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;

    }


}
}

下面是创建 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);

            }


    });

}
}

非常感谢你们!=)

4

2 回答 2

3
 // 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);

您启动了两次 TonosSet 活动。第一次启动它时,意图没有额外的。要解决您的问题,请检查您的意图是否有额外的。

Intent i = getIntent();
int position = -1; // init with default value
if(i.getExtras() != null)
    position = i.getExtras().getInt("id");
于 2012-10-05T20:50:16.180 回答
1
            Intent tonosset = new Intent(Tonos.this, TonosSet.class); 
            startActivity(tonosset); 

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

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

您正在创建两个意图:一个有额外的,另一个没有。当您尝试从没有的活动中获得额外的东西时,就会发生 NPE。

于 2012-10-05T20:55:37.233 回答