2012-08-12 73 views
0

我得到一個空指針異常,我在其中指出我的其他(谷歌)活動。在我的內部類MyEventHandler的onclick方法中調用了startActivity(go)。請告訴我哪裏出錯了。我是android新手。意圖中的空指針異常

**package pack.Assignment;** 

public class Landing extends Activity { 
// url to make request 

private static String url = "http://playup-jo.s3.amazonaws.com/dev/config.json"; 
private ProgressDialog pDialog; 
ImageView img; 
// tiles JSONArray 
JSONArray tiles=null; 
private String server; 
Intent go = new Intent(getApplicationContext(), Google.class); 

// JSON Node names 
private static final String TAG_SERVER = "server"; 
private static final String TAG_TILES = "tiles"; 
private static final String TAG_IMAGE = "image"; 
private static final String TAG_URL = "url"; 
private static final String TAG_MDPI = "mdpi"; 
private static final String TAG_NAME = "name"; 


@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.landing); 
    // Loading JSON in Background Thread 
    new LoadJSON().execute(); 

} 
/** 
* Background Async Task to Load all INBOX messages by making HTTP Request 
* */ 
class LoadJSON extends AsyncTask<String, String, Bitmap> { 
    /** 
    * Before starting background thread Show Progress Dialog 
    * */ 
    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     pDialog = new ProgressDialog(Landing.this); 
     pDialog.setMessage("Loading ..."); 
     pDialog.setIndeterminate(false); 
     pDialog.setCancelable(false); 
     pDialog.show(); 
    } 

    /** 
    * getting JSON 
    * */ 
    protected Bitmap doInBackground(String... args) { 

     // getting JSON string from URL 
     JSONParser jParser = new JSONParser(); 

     JSONObject json = jParser.getJSONFromUrl(url); 
     Bitmap bitmap=null; 


     try { 
      server = json.getString("server"); 
      // Getting Array of Tiles 
      tiles = json.getJSONArray(TAG_TILES); 


      // looping through All Tiles 
      for(int i = 0; i < tiles.length(); i++){ 
       JSONObject c = tiles.getJSONObject(i); 

       // Storing json item in variable 
       String name = c.getString(TAG_NAME); 
       System.out.println("name :"+name); 

       // mdpi is again JSONObject 
       JSONObject mdpi= c.getJSONObject(TAG_MDPI); 
       String image=mdpi.getString(TAG_IMAGE); 
       String image_url = server+image; 
       String url=mdpi.getString(TAG_URL); 

       // Using the variable to get the bitmap 

       try { 
         bitmap = BitmapFactory.decodeStream((InputStream)new URL(image_url).getContent()); 
        } 
       catch (MalformedURLException e) { 
         e.printStackTrace(); 
        } 
       catch (IOException e) { 
         e.printStackTrace(); 
        }      
      } 

     } 
     catch (JSONException e) { 
      e.printStackTrace(); 
    } 

     return bitmap; 
    } 

    /** 
    * After completing background task Dismiss the progress dialog 
    * **/ 
    protected void onPostExecute(final Bitmap result) { 
     // dismiss the dialog after getting all products 
     pDialog.dismiss(); 
     img = (ImageView)findViewById(R.id.image); 

     img.setImageBitmap(result); 
     MyEventHandler myEvHandler = new MyEventHandler(); 
     // making the downloaded image clickable 
     img.setOnClickListener(myEvHandler); 
    } 


} 
class MyEventHandler implements OnClickListener 
{ 
    public void onClick(View v) 
    { 
     if (v instanceof ImageView) 
     { 

      startActivity(go); 
     }  
    } 


} 

}

回答

2

也許ü應該初始化裏面的onCreate意圖。它只是一個預感雖然但試一試,雖然

+0

工作。 thanx – Feona 2012-08-12 11:56:59

0

你的問題實際上並沒有創建它的意圖其調用方法getApplicationContext()。這應該在適當的時候被調用。這樣做一次onCreate()已被調用。

+0

當我將意圖語句放在'onCreate()'語句中時,空指針異常就解決了。 – Feona 2012-08-12 12:02:52

+2

是的,我知道,但這純粹是因爲你在錯誤的地方調用了'getApplicationContext()',它與創建Intent本身無關。它是實際導致空指針的'getApplicationContext()'。 – 2012-08-12 12:04:13

+0

是的,這就是爲什麼你得到空指針的全部細節 – lemoncodes 2012-08-12 12:18:43