0

私のアプリケーションは私に FATAL EXCEPTION: AsyncTask #2 エラーを与えているようです.誰かが私のコードを見て何が間違っているのか教えてくれませんか.

スタックトレース

07-27 07:58:42.301: E/AndroidRuntime(926): FATAL EXCEPTION: AsyncTask #2
07-27 07:58:42.301: E/AndroidRuntime(926): java.lang.RuntimeException: An error occured while executing doInBackground()
07-27 07:58:42.301: E/AndroidRuntime(926):  at android.os.AsyncTask$3.done(AsyncTask.java:299)
07-27 07:58:42.301: E/AndroidRuntime(926):  at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
07-27 07:58:42.301: E/AndroidRuntime(926):  at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
07-27 07:58:42.301: E/AndroidRuntime(926):  at java.util.concurrent.FutureTask.run(FutureTask.java:239)
07-27 07:58:42.301: E/AndroidRuntime(926):  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
07-27 07:58:42.301: E/AndroidRuntime(926):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
07-27 07:58:42.301: E/AndroidRuntime(926):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
07-27 07:58:42.301: E/AndroidRuntime(926):  at java.lang.Thread.run(Thread.java:856)
07-27 07:58:42.301: E/AndroidRuntime(926): Caused by: java.lang.NullPointerException
07-27 07:58:42.301: E/AndroidRuntime(926):  at com.theproblemsolver.ListView$LoadAllData.doInBackground(ListView.java:92)
07-27 07:58:42.301: E/AndroidRuntime(926):  at com.theproblemsolver.ListView$LoadAllData.doInBackground(ListView.java:1)
07-27 07:58:42.301: E/AndroidRuntime(926):  at android.os.AsyncTask$2.call(AsyncTask.java:287)
07-27 07:58:42.301: E/AndroidRuntime(926):  at java.util.concurrent.FutureTask.run(FutureTask.java:234)
07-27 07:58:42.301: E/AndroidRuntime(926):  ... 4 more

主な活動

public class MainActivity extends Activity {

    EditText et;
    Button getanswer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button getanswer = (Button) findViewById(R.id.button1);
        getanswer.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent i = new Intent(MainActivity.this, ListView.class);
            startActivity(i);
        }


        });
    } 
}

ListView アクティビティ

public class ListView extends ListActivity {

    private ProgressDialog pDialog;

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

    private static String URI = "http://example.com/json";;

    //JSONObject json = jParser.getJSONFromURI(URI); 

     final String TAG_RESULTS = "ResultsSet";
     final String TAG_SUBJECT = "Subject";
     final String TAG_NUMANSWERS = "NumAnswers";
     final String TAG_QUESTION = "Question";
     final String TAG_QUESTION_CONTENT = "Content";
     final String TAG_QUESTION_CHOSENANSWER = "ChosenAnswer";
     final String TAG_QUESTION_ANSWERS = "Answers";

            JSONArray ResultsSet = null;


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

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

    new LoadAllData().execute();

    android.widget.ListView lv = getListView();

    lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,int position, long id) {


        }
    });

}

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

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

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

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(ListView.this);
            pDialog.setMessage("Loading Data. Please wait...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }

        protected String doInBackground(String... args) {

            try {
                    JSONObject json = jParser.getJSONFromURI(URI);
                    ResultsSet = json.getJSONArray(TAG_RESULTS);

                   for(int i = 0; i < ResultsSet.length(); i++) {
                       JSONObject r = ResultsSet.getJSONObject(i);
                       String Subject = r.getString(TAG_SUBJECT);
                       String NumAnswers = r.getString(TAG_NUMANSWERS);

                       JSONObject Question = r.getJSONObject(TAG_QUESTION);
                       String Content = Question.getString(TAG_QUESTION_CONTENT);
                       String ChosenAnswer = Question.getString(TAG_QUESTION_CHOSENANSWER);
                       String Answers = Question.getString(TAG_QUESTION_ANSWERS);

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

                       map.put(TAG_SUBJECT, Subject);
                       map.put(TAG_NUMANSWERS, NumAnswers);
                   }



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


        }


        protected void onPostExecute(String file_URI) {

            pDialog.dismiss();
            runOnUiThread(new Runnable() {
            public void run() {
                ListAdapter adapter = new SimpleAdapter(getBaseContext(), questionList,
                        R.layout.row,
                        new String[] { TAG_SUBJECT, TAG_NUMANSWERS }, new int[] {
                        R.id.Subject, R.id.NumAnswers });

                setListAdapter(adapter);



                }
            });

        }
}}

SingleListItem アクティビティ

public class SingleListItem extends Activity {

    TextView subject;

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

    subject = (TextView) findViewById(R.id.Subject2);

    Intent i = getIntent();
    String Subject = i.getStringExtra("TAG_SUBJECT");
    subject.setText(Subject);

    }
}
4

1 に答える 1

3

リストビューの初期化前にロード データを実行しています。

最初にリストビューを初期化してからデータをロードします。お気に入り:

android.widget.ListView lv = getListView();

new LoadAllData().execute();
于 2013-07-27T08:34:32.027 に答える