2013-03-29 32 views
0

我对Main方法有问题。我有一个叫做studentLife的按钮,无论我点击它,它都会带有一个带有图片的网格视图。这部分工作正常,但当我点击一个特定的图像,我想带FullImage.class。当我点击我得到一个图像Android应用程序已停止

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

我的代码(GMITApp已经停了!):

@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 [email protected]f36d8 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) 
+0

检查你的android logCat。在这里你可以找到你的例外行号。 – WELLCZECH

+0

我在第141行得到了一个nullPointerException,它是:Intent i = new Intent(getApplicationContext(),FullImage.class) – ciastkoo

+0

@WiolaStępniak:plz完整日志与问题获得更多帮助 –

回答

1

替换getApplicationContext()YOUR_ACTIVITY_NAME.this

P.S .:在询问问题时,请始终与您的代码共享logCat信息。

+0

不起作用 - log cat在编辑过的文章 – ciastkoo

+0

当然,它不起作用,因为'getAppContext'只返回实际的上下文,所以在这种情况下,如果你使用'getapp..'或'this',它是相同的。 – WELLCZECH

+0

@WELLCZECH:您的logCat表明您在使用AsyncTask时遇到了问题。你在用吗? –