2015-04-02 100 views
-1

我正在使用异步任务将json检索到baseadapter并最终将其显示到listview中。我无法做到这一点,我得到错误,我的应用程序崩溃。 这里是我的代码: MainActivity.java无法检索JSON

private String jsonString, url, username; 
JSONArray JArray; 
ImageView profileimg; 
ListView lv; 
BaseAdapterTest adapter; 
private JSONArray questions; 
JSONObject temp_obj; 
ArrayList<String> test = new ArrayList<String>(); 
public static String Tag_profile_pic = "profile_pic"; 
public static String Tag_username = "username"; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.nnj); 
    lv = (ListView) findViewById(R.id.listview); 
      new GetJson().execute(); 


} 

; 

private class GetJson extends AsyncTask<Void, Void, Void> { 
    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 

    } 


    @Override 
    protected Void doInBackground(Void... params) { 
     ServiceHandler http = new ServiceHandler(); 
     jsonString = http.makeServiceCall(url, ServiceHandler.GET, 
       null); 
     if (jsonString != null) { 
      try { 

       // Getting JSON Array node 
       JArray = new JSONArray(jsonString); 

       // looping through All Contacts 
       for (int i = 0; i < JArray.length(); i++) { 
        temp_obj = JArray.getJSONObject(i); 
        JSONObject currentObject = new JSONObject(); 
        currentObject.put("profile_pic", temp_obj.getString(Tag_profile_pic) 
          .toString()); 
        currentObject.put("username", temp_obj.getString(Tag_username) 
          .toString()); 
        JArray.put(currentObject); 
       } 

      } catch (JSONException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } else { 
      Log.d("TAG", "Empty"); 
     } 
     return null; 
    } 

    @Override 
    protected void onPostExecute(Void result) { 
     // TODO Auto-generated method stub 
     super.onPostExecute(result); 
     Toast.makeText(getApplicationContext(), JArray + "", Toast.LENGTH_LONG).show(); 
     adapter = new BaseAdapterTest(MainActivity.this, JArray) { 
      @Override 
      public int getCount() { 
       return 0; 
      } 

      @Override 
      public Object getItem(int position) { 
       return null; 
      } 

      @Override 
      public long getItemId(int position) { 
       return 0; 
      } 

      @Override 
      public View getView(int position, View convertView, ViewGroup parent) { 
       return null; 
      } 
     }; 
     // Set the adapter to the ListView 
     lv.setAdapter(adapter); 
    } 


} 

}

和BaseAdapter.java

public class BaseAdapterTest extends android.widget.BaseAdapter { 

// Declare Variables 
Context context; 
LayoutInflater inflater; 
Array JArray; 


public BaseAdapterTest(MainActivity mainActivity, JSONArray JArray) { 
} 

@Override 
public int getCount() { 
    return getCount(); 
} 

@Override 
public Object getItem(int position) { 
    return null; 
} 

@Override 
public long getItemId(int position) { 
    return 0; 
} 

public View getView(final int position, View convertView, ViewGroup parent) { 
    // Declare Variables 
    TextView username; 
    ImageView profilepic; 

    inflater = (LayoutInflater) context 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    View itemView = inflater.inflate(R.layout.listview, parent, false); 
    // Locate the TextViews in listview_item.xml 
    username = (TextView) itemView.findViewById(R.id.questionfeed_customelistview_txt_username); 

    // Locate the ImageView in listview_item.xml 
    profilepic = (ImageView) itemView.findViewById(R.id.questionfeed_customelistview_imgv_follow); 

    itemView.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 
      // Get the position 
      Intent intent = new Intent(context, MainActivity.class); 
      // Pass all data rank 
      intent.putExtra("username", (MainActivity.Tag_username)); 
      // Pass all data flag 
      intent.putExtra("profilepic",(MainActivity.Tag_profile_pic)); 
      // Start SingleItemView Class 
      context.startActivity(intent); 

     } 
    }); 
    return itemView; 
} 

}

的代码,这是我的logcat

04-02 15:49:31.871 2935-2935/com.cheerz.json E/AndroidRuntime﹕ FATAL EXCEPTION: main 
Process: com.cheerz.json, PID: 2935 
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference 
     at com.cheerz.json.MainActivity$GetJson.onPostExecute(MainActivity.java:115) 
     at com.cheerz.json.MainActivity$GetJson.onPostExecute(MainActivity.java:47) 
     at android.os.AsyncTask.finish(AsyncTask.java:632) 
     at android.os.AsyncTask.access$600(AsyncTask.java:177) 
     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645) 
     at android.os.Handler.dispatchMessage(Handler.java:102) 
     at android.os.Looper.loop(Looper.java:135) 
     at android.app.ActivityThread.main(ActivityThread.java:5312) 
     at java.lang.reflect.Method.invoke(Native Method) 
     at java.lang.reflect.Method.invoke(Method.java:372) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696) 
+0

和错误是什么? – NSimon 2015-04-02 10:29:16

+0

发布您的错误日志 – 2015-04-02 10:29:52

+0

等我也给日志猫 – 2015-04-02 10:30:11

回答

-1

更换

JArray = new JSONArray(jsonString); 

JSONObject jsonobject = new JSONObject(jsonString); 
JArray = ((JSONObject) jsonobject).getJSONArray("key_name_of_your_array"); 
+0

有意思......告诉我们为什么他应该这样做? ...如果'jsonString'包含数组作为根,你的代码没有敏感......他没有发布'jsonString'包含的所以... sherlock,请告诉我们你是如何推断出来的......或者你只是喜欢[通过排列编程](http://en.wikipedia.org/wiki/Programming_by_permutation) – Selvin 2015-04-02 10:44:58

+0

其发生相同。我的应用程序仍在崩溃。 – 2015-04-02 10:50:30

+0

你可以请我提供你在jsonString中获得什么? – 2015-04-02 10:52:59