0

我是 AsyncTask 的新手,所以遇到了几个错误。我只是有人告诉我出了什么问题以及我需要做些什么来解决它。每次尝试此代码时,我都会收到一个 FATAL EXCEPTION: AsyncTask #1 所以有人可以帮助我。

 07-25 03:47:57.526: W/dalvikvm(799): threadid=11: thread exiting with uncaught exception (group=0x40a71930)
    07-25 03:47:57.666: E/AndroidRuntime(799): FATAL EXCEPTION: AsyncTask #1
    07-25 03:47:57.666: E/AndroidRuntime(799): java.lang.RuntimeException: An error occured while executing doInBackground()
    07-25 03:47:57.666: E/AndroidRuntime(799):  at android.os.AsyncTask$3.done(AsyncTask.java:299)
    07-25 03:47:57.666: E/AndroidRuntime(799):  at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
    07-25 03:47:57.666: E/AndroidRuntime(799):  at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
    07-25 03:47:57.666: E/AndroidRuntime(799):  at java.util.concurrent.FutureTask.run(FutureTask.java:239)
    07-25 03:47:57.666: E/AndroidRuntime(799):  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
    07-25 03:47:57.666: E/AndroidRuntime(799):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
    07-25 03:47:57.666: E/AndroidRuntime(799):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
    07-25 03:47:57.666: E/AndroidRuntime(799):  at java.lang.Thread.run(Thread.java:856)
    07-25 03:47:57.666: E/AndroidRuntime(799): Caused by: java.lang.NullPointerException
    07-25 03:47:57.666: E/AndroidRuntime(799):  at com.theproblemsolver.JSONParsser.getJSONFromURI(JSONParsser.java:35)
    07-25 03:47:57.666: E/AndroidRuntime(799):  at com.theproblemsolver.asynctask.doInBackground(MainActivity.java:60)
    07-25 03:47:57.666: E/AndroidRuntime(799):  at com.theproblemsolver.asynctask.doInBackground(MainActivity.java:1)
    07-25 03:47:57.666: E/AndroidRuntime(799):  at android.os.AsyncTask$2.call(AsyncTask.java:287)
    07-25 03:47:57.666: E/AndroidRuntime(799):  at java.util.concurrent.FutureTask.run(FutureTask.java:234)

主要活动

public class MainActivity extends Activity {

    EditText et;
    Button getanswer;

    JSONArray ResultsSet = null; {}

    @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) {
            new asynctask().execute();

        }

        });
    } 
}

      class asynctask extends AsyncTask<String, Void, JSONObject> {

             private static final String TAG_RESULTS = "ResultsSet";
             private static final String TAG_SUBJECT = "Subject";
             private static final String TAG_NUMANSWERS = "NumAnswers";
             private static final String TAG_QUESTION = "Question";
             private static final String TAG_QUESTION_CONTENT = "Content";
             private static final String TAG_QUESTION_CHOSENANSWER = "ChosenAnswer";
             private static final String TAG_QUESTION_ANSWERS = "Answers";
            public JSONArray json;

         @Override
         protected JSONObject doInBackground(String... params) {

             JSONParsser jParser = new JSONParsser();

             String uRI = null;

             JSONObject json = jParser.getJSONFromURI(uRI);

             return json ;

         }{ }
             @Override
             protected void onPostExecute(JSONObject json) {

                 JSONArray ResultsSet = null; {
                 try {
                     ResultsSet = json.getJSONArray(TAG_RESULTS);
                } catch (JSONException e) {
                   e.printStackTrace();
             }

            return;

        }}}

JSONParser 类

public class JSONParsser {

    static InputStream is = null;
    static JSONObject jObj = null;
    static String json = "";
    EditText et;

    public JSONParsser () {
    }

    public JSONObject getJSONFromURI(String uRI) {

        try{
        HttpClient client = new DefaultHttpClient();
        try {
            URI website = new URI("http://answers.yahooapis.com/AnswerService/V1/questionSearch?appid=vcE15nV34EYK7oAge17Ws6nF3DOzc5ccb4PsWtQjW2YTG3gjKbz8exOHYQr6Psizg&query=" + et.getText() + "sort&type=resolved&results&output=json");
            HttpGet request = new HttpGet();
            request.setURI(website);
            try {
                HttpResponse response = client.execute(request);
            HttpEntity httpEntity = response.getEntity();
            is = httpEntity.getContent();

            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } catch (URISyntaxException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        return jObj;

        }finally{}

    }{

    }}

JSONParser 类的实例

public class JSONParserInstance {

    JSONParsser jParser = new JSONParsser();

    public String uRI;

    JSONObject json = jParser.getJSONFromURI(uRI);


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

            JSONArray ResultsSet = null; {

    try {
       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 NmAnswers = 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);

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

}}}
4

3 回答 3

2
String uRI = null;

JSONObject json = jParser.getJSONFromURI(uRI);

您正在尝试解析 null-String 并且您正在获取 java.lang.NullPointerException

尝试new asynctask().execute(someUrl);改为在您的 doInBackground 方法中new asynctask().execute();更改String uRI = null;为。String uRI = params[0]

于 2013-07-25T04:55:08.120 回答
0

在您的代码中,您将null值传递给getJSONFromURI(),这导致空指针异常

String uRI = null;

JSONObject json = jParser.getJSONFromURI(uRI);  // Passing null 
于 2013-07-25T04:54:12.470 回答
0

改变

@Override
     protected JSONObject doInBackground(String... params) {

         JSONParsser jParser = new JSONParsser();

         String uRI = null;

         JSONObject json = jParser.getJSONFromURI(uRI);

         return json ;

     }{ }

 String uRI = null;//don't make it null
@Override
         protected JSONObject doInBackground(String... params) {

             JSONParsser jParser = new JSONParsser();



             JSONObject json = jParser.getJSONFromURI(uRI);

             return json ;

        }
于 2013-07-25T05:00:51.430 回答