我得到一个空指针异常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.类:
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() {