返回NPE的Android简单适应器

时间:2013-08-09 01:24:36

标签: android android-listview

我有一点Listview适配器问题。我很高兴地说我已经将我的Web服务部分工作了,但现在我的Listview中的简单适配器似乎有问题。当应用程序返回Web服务数据并尝试在列表视图中显示它时,我得到一个NPE。我看看stackoverflow是否有任何与我有关的问题,我找到了一对,但他们在我的情况下并没有真正帮助。所以我只是希望有人能在我的情况下看到这个问题并给我一些建议。

这是我的logcat:

08-09 00:19:37.415: E/AndroidRuntime(770): FATAL EXCEPTION: main
08-09 00:19:37.415: E/AndroidRuntime(770): java.lang.NullPointerException
08-09 00:19:37.415: E/AndroidRuntime(770):  at android.widget.SimpleAdapter.getCount(SimpleAdapter.java:93)
08-09 00:19:37.415: E/AndroidRuntime(770):  at android.widget.ListView.setAdapter(ListView.java:462)
08-09 00:19:37.415: E/AndroidRuntime(770):  at android.app.ListActivity.setListAdapter(ListActivity.java:265)
08-09 00:19:37.415: E/AndroidRuntime(770):  at com.theproblemsolver.ListView$LoadAllData.onPostExecute(ListView.java:142)
08-09 00:19:37.415: E/AndroidRuntime(770):  at com.theproblemsolver.ListView$LoadAllData.onPostExecute(ListView.java:1)
08-09 00:19:37.415: E/AndroidRuntime(770):  at android.os.AsyncTask.finish(AsyncTask.java:631)
08-09 00:19:37.415: E/AndroidRuntime(770):  at android.os.AsyncTask.access$600(AsyncTask.java:177)
08-09 00:19:37.415: E/AndroidRuntime(770):  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
08-09 00:19:37.415: E/AndroidRuntime(770):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-09 00:19:37.415: E/AndroidRuntime(770):  at android.os.Looper.loop(Looper.java:137)
08-09 00:19:37.415: E/AndroidRuntime(770):  at android.app.ActivityThread.main(ActivityThread.java:5041)
08-09 00:19:37.415: E/AndroidRuntime(770):  at java.lang.reflect.Method.invokeNative(Native Method)
08-09 00:19:37.415: E/AndroidRuntime(770):  at java.lang.reflect.Method.invoke(Method.java:511)
08-09 00:19:37.415: E/AndroidRuntime(770):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-09 00:19:37.415: E/AndroidRuntime(770):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-09 00:19:37.415: E/AndroidRuntime(770):  at dalvik.system.NativeStart.main(Native Method)

的ListView:

public class ListView extends ListActivity {    


    ArrayList<HashMap<String, String>> questionList;    

     final String TAG_RESULTS = "results";
     final String TAG_QUESTION_SUBJECT = "Subject";
     final String TAG_QUESTION_NUMANSWERS = "NumAnswers";
     final String TAG_QUESTION = "Question";
     final String TAG_QUESTION_CONTENT = "Content";
     final String TAG_QUESTION_CHOSENANSWER = "ChosenAnswer";
     final String TAG_ANSWERS = "Answers";
     final String TAG_ANSWER = "Answer";    
     final String TAG_ANSWERS_CONTENT = "Content";      

            JSONArray results;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); 
        //setContentView(R.layout.listview);        

         //questionList = new ArrayList<HashMap<String, String>>();



    new LoadAllData().execute();
        }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == 100) {
            Intent intent = getIntent();
            startActivity(intent);
            finish();
        }
    }

    class LoadAllData extends AsyncTask<String, String, String> {


        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            ProgressDialog pDialog; 
            pDialog = new ProgressDialog(ListView.this);
            pDialog.setMessage("Loading Data. Please wait...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
            if (pDialog != null && pDialog.isShowing()) pDialog.dismiss();

        }

        protected String doInBackground(String... args) {

            try {
                Intent in = getIntent();
                String searchTerm = in.getStringExtra("TAG_SEARCH");
                String query = URLEncoder.encode(searchTerm, "utf-8");
                String URL = "http://example.com/json";
                JSONParsser jParser = new JSONParsser();
                JSONObject json = jParser.readJSONFeed(URL);
                try {
                    results = json.getJSONArray(TAG_RESULTS);

                    for(int i = 0; i < results.length(); i++) {
                           JSONObject r = results.getJSONObject(i);

                           JSONObject Question = r.getJSONObject(TAG_QUESTION);
                           String Subject = Question.getString(TAG_QUESTION_SUBJECT);
                           String NumAnswers = Question.getString(TAG_QUESTION_NUMANSWERS);
                           String ChosenAnswers= Question.getString(TAG_QUESTION_CHOSENANSWER);
                           String Content = Question.getString(TAG_QUESTION_CONTENT);

                           //JSONObject Answers = Question.getJSONObject(TAG_ANSWERS);
                           //JSONObject Answer = Answers.getJSONObject(TAG_ANSWER);
                           //String Content1 = Answers.getString(TAG_ANSWERS_CONTENT);

                             questionList = new ArrayList<HashMap<String, String>>();

                               HashMap<String, String> map = new HashMap<String, String>();

                               map.put(TAG_QUESTION_SUBJECT, Subject);
                               map.put(TAG_QUESTION_NUMANSWERS, NumAnswers);

                               questionList.add(map);

                           }



                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

                return TAG_RESULTS ; 



        }
        @Override
        protected void onPostExecute(String file_URL) {

                ListAdapter adapter = new SimpleAdapter(getBaseContext(), questionList,
                        R.layout.listview,
                        new String[] { TAG_QUESTION_SUBJECT, TAG_QUESTION_NUMANSWERS }, new int[] {
                        R.id.Subject, R.id.NumAnswers });

                setListAdapter(adapter);

                android.widget.ListView lv = getListView();

                lv.setOnItemClickListener(new OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> arg0, View arg1,int arg2, long arg3) {
                        // TODO Auto-generated method stub

                    }   

                });     

        }}

    }

listview.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

      <TextView
        android:id="@+id/Subject"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView 
        android:id="@+id/NumAnswers"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />


    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ScrollView>

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>

我的logcat也显示此系统错误:

08-09 02:35:05.806: W/System.err(1586): org.json.JSONException: No value for results
08-09 02:35:05.806: W/System.err(1586):     at org.json.JSONObject.get(JSONObject.java:354)
08-09 02:35:05.816: W/System.err(1586):     at org.json.JSONObject.getJSONArray(JSONObject.java:544)
08-09 02:35:05.816: W/System.err(1586):     at com.theproblemsolver.ListView$LoadAllData.doInBackground(ListView.java:91)
08-09 02:35:05.826: W/System.err(1586):     at com.theproblemsolver.ListView$LoadAllData.doInBackground(ListView.java:1)
08-09 02:35:05.826: W/System.err(1586):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
08-09 02:35:05.826: W/System.err(1586):     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
08-09 02:35:05.826: W/System.err(1586):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
08-09 02:35:05.836: W/System.err(1586):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
08-09 02:35:05.836: W/System.err(1586):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
08-09 02:35:05.836: W/System.err(1586):     at java.lang.Thread.run(Thread.java:856)

0 个答案:

没有答案