0

我对 Main 方法有疑问。我有一个名为 studentLife 的按钮,无论我在何处单击它,它都会带来一个带有图片的网格视图。那部分工作正常,但是当我单击特定图像时,我想带上 FullImage.class。当我单击图像时(GMITApp 已停止!)

I got a nullPointerException at line 141 which is: 
          Intent i = new Intent(getApplicationContext(), FullImage.class);

我的代码:

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final Button studentLife = (Button) findViewById(R.id.student_life);
    //declare a grid view variable 
    GridView gridView = (GridView)findViewById(R.id.grid_view);

    studentLife.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            setContentView(R.layout.activity_gallery);
            GridView gridView = (GridView) findViewById(R.id.grid_view);
            // instance of ImageAdapter class
            gridView.setAdapter(new ImageAdapter(getApplicationContext()));

        }
    }); // end of studentLife.setOnClickListener




    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(), FullImage.class);
            // passing array index
            i.putExtra("id", position);
            startActivity(i);       
        }
    });


}// end of onCreate

这是图像类:

    public class FullImage extends Activity {


    @Override
    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.activity_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);
    }


}

这是我更改为时得到getApplicationContext()MainActivity.this

03-29 12:34:05.523: E/StrictMode(635): null
03-29 12:34:05.523: E/StrictMode(635): android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40cf36d8 that was originally bound here
03-29 12:34:05.523: E/StrictMode(635):  at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
03-29 12:34:05.523: E/StrictMode(635):  at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
03-29 12:34:05.523: E/StrictMode(635):  at android.app.ContextImpl.bindService(ContextImpl.java:1418)
03-29 12:34:05.523: E/StrictMode(635):  at android.app.ContextImpl.bindService(ContextImpl.java:1407)
03-29 12:34:05.523: E/StrictMode(635):  at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
03-29 12:34:05.523: E/StrictMode(635):  at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
03-29 12:34:05.523: E/StrictMode(635):  at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
03-29 12:34:05.523: E/StrictMode(635):  at com.android.emailcommon.service.ServiceProxy.test(ServiceProxy.java:191)
03-29 12:34:05.523: E/StrictMode(635):  at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1850)
03-29 12:34:05.523: E/StrictMode(635):  at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
03-29 12:34:05.523: E/StrictMode(635):  at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
4

1 回答 1

1

替换getApplicationContext()YOUR_ACTIVITY_NAME.this

PS:请在提问时始终与您的代码一起分享 logCat 信息。

于 2013-03-29T12:28:23.313 回答