でヌルポインタ例外が発生していgridView.setOnItemClickListener(new OnItemClickListener()
ます。どうすれば初期化できますか? これは MainActivity の `onCreate メソッド内にあります。
GridView gridView = (GridView)findViewById(R.id.grid_view);
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v, int position,
long id) {
// sending image id to FullScreenActivity
Intent i = new Intent(getApplicationContext(), FullImage2.class);
// passing array index
i.putExtra("id", position);
startActivity(i);
}
});
それはログです:
03-30 11:51:31.993: E/AndroidRuntime(879): FATAL EXCEPTION: main
03-30 11:51:31.993: E/AndroidRuntime(879): java.lang.RuntimeException: Unable to start activity ComponentInfo{ie.gmit.gmitapp/ie.gmit.gmitapp.MainActivity}: java.lang.NullPointerException
03-30 11:51:31.993: E/AndroidRuntime(879): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
03-30 11:51:31.993: E/AndroidRuntime(879): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-30 11:51:31.993: E/AndroidRuntime(879): at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-30 11:51:31.993: E/AndroidRuntime(879): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-30 11:51:31.993: E/AndroidRuntime(879): at android.os.Handler.dispatchMessage(Handler.java:99)
03-30 11:51:31.993: E/AndroidRuntime(879): at android.os.Looper.loop(Looper.java:137)
03-30 11:51:31.993: E/AndroidRuntime(879): at android.app.ActivityThread.main(ActivityThread.java:5039)
03-30 11:51:31.993: E/AndroidRuntime(879): at java.lang.reflect.Method.invokeNative(Native Method)
03-30 11:51:31.993: E/AndroidRuntime(879): at java.lang.reflect.Method.invoke(Method.java:511)
03-30 11:51:31.993: E/AndroidRuntime(879): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-30 11:51:31.993: E/AndroidRuntime(879): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-30 11:51:31.993: E/AndroidRuntime(879): at dalvik.system.NativeStart.main(Native Method)
03-30 11:51:31.993: E/AndroidRuntime(879): Caused by: java.lang.NullPointerException
03-30 11:51:31.993: E/AndroidRuntime(879): at ie.gmit.gmitapp.MainActivity.onCreate(MainActivity.java:184)
03-30 11:51:31.993: E/AndroidRuntime(879): at android.app.Activity.performCreate(Activity.java:5104)
03-30 11:51:31.993: E/AndroidRuntime(879): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
03-30 11:51:31.993: E/AndroidRuntime(879): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
FullImage2.class:
public class FullImage2 extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_full_image);
//get intent data
Intent i = getIntent();
// selected image id
int position = i.getExtras().getInt("id");
ImageAdapter imageAdapter = new ImageAdapter(this);
ImageView imageView = (ImageView)findViewById(R.id.full_image_view);
imageView.setImageResource(imageAdapter.mThumbIds[position]);
//implement up navigation
getActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.full_image, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()){
case android.R.id.home:
//this is called when the home up button is pressed in the action bar
Intent parentActivityIntent = new Intent(this, MainActivity.class);
parentActivityIntent.addFlags(
Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(parentActivityIntent);
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
}
184行目は次のとおりです。
gridView.setOnItemClickListener(new OnItemClickListener() {