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);
}
});
}
}
どうもありがとう!=)