我在我的 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);
}
});
}
}
非常感谢你们!=)