2012-08-11 38 views
-1

我在代码中显示的三行中出现空行指针异常。请看看它。我是Android的初学者。提前致谢。我尝试实例化JSON数组,但即使这样也行不通。JSON程序中的空指针异常

package pack.assignment;   *** 
import java.net.URL; 
import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 
import java.io.InputStream; 
import android.content.Intent; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.widget.Toast; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import java.io.IOException; 
import android.widget.ImageView; 
import java.net.MalformedURLException; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.app.Activity; 
import android.app.ProgressDialog; 

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; 
    JSONParser jParser = new JSONParser(); 
    // tiles JSONArray 
    JSONArray tiles=null; 
    ImageView img; 
    // JSON Node names 
    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"; 

    // Intent go = new Intent(this, Google.class); 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     // Loading JSON in Background Thread 
     new LoadJSON().execute(); 
     img = (ImageView)findViewById(R.id.image); 

     // getting JSON string from URL 
     JSONObject json = jParser.getJSONFromUrl(url); 
    } 
    /** 
    * 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 
      JSONObject json = jParser.getJSONFromUrl(url); 
      Bitmap bitmap=null; 
      // Check your log cat for JSON reponse 
     // Log.d("JSONNN: ", json.toString()); 

      try { 
       // Getting Array of Tiles 
       tiles= new JSONArray(); 
       tiles = json.getJSONArray(TAG_TILES);   *** 

       // looping through All Tiles 
       for(int i = 0; i < 1; i++){ 
        JSONObject c = tiles.getJSONObject(i); 
        // Storing json item in variable 
        String name = c.getString(TAG_NAME); 
        // mdpi is again JSONObject 
        JSONObject mdpi= c.getJSONObject(TAG_MDPI); 
        String image=mdpi.getString(TAG_IMAGE); 
        String url=mdpi.getString(TAG_URL); 
        // Using the variable to get the bitmap 
        try { 
          bitmap = BitmapFactory.decodeStream((InputStream)new URL(image).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(); 
      // updating UI from Background Thread 
      runOnUiThread(new Runnable() { 
       public void run() { 
        /** 
        * Updating parsed JSON data 
        * */ 
         img.setImageBitmap(result);  // Rule:2 Always access UI toolkit for UI thread not worker thread 
         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); 
      }  
     } 


    } 
} 
+0

什么是JSONParser这里?如果是你的课程,请发帖 – 2012-08-13 09:05:46

+0

的代码你的权利。错误发生在JSONParser文件中,我使用的是httpPost而不是httpGet。我的问题是,如果我使用httpPost,我还可以使它工作吗?从技术上讲,它也应该起作用。不应该吗? – Feona 2012-08-13 09:35:41

回答

0

错误是在JSONParser类,我用httpPost和获得的405状态码,而不是200.我用HTTPGET和它得到了解决。