0

我在对我的其他(谷歌)活动做出意图的行中收到一个空指针异常。startActivity(go)在我的内部类 MyEventHandler 的 onclick 方法中调用。请告诉我哪里出错了。我是安卓新手。

**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);
        }       
    }


}

}

4

2 回答 2

2

也许你应该在 onCreate 中初始化意图。虽然这只是一种预感,但请尝试一下

于 2012-08-12T11:50:49.033 回答
0

您的问题实际上并不是在创建调用方法的意图getApplicationContext()。这应该在适当的时候调用。这样做一次onCreate()已被调用。

于 2012-08-12T11:57:11.493 回答