0

我设法通过编码我的编辑文本来解决我的 JSONObject 问题,但我的应用程序仍然强制崩溃。这次我的 logcat 说我在错误的线程中调用了我的编辑文本,所以我只是希望比我有更好经验的人能给我一些建议并告诉我可以做些什么来解决这个问题。

08-05 19:07:36.617: E/AndroidRuntime(772): FATAL EXCEPTION: AsyncTask #2
08-05 19:07:36.617: E/AndroidRuntime(772): java.lang.RuntimeException: An error occured while executing doInBackground()
08-05 19:07:36.617: E/AndroidRuntime(772):  at android.os.AsyncTask$3.done(AsyncTask.java:299)
08-05 19:07:36.617: E/AndroidRuntime(772):  at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
08-05 19:07:36.617: E/AndroidRuntime(772):  at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
08-05 19:07:36.617: E/AndroidRuntime(772):  at java.util.concurrent.FutureTask.run(FutureTask.java:239)
08-05 19:07:36.617: E/AndroidRuntime(772):  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
08-05 19:07:36.617: E/AndroidRuntime(772):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
08-05 19:07:36.617: E/AndroidRuntime(772):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
08-05 19:07:36.617: E/AndroidRuntime(772):  at java.lang.Thread.run(Thread.java:856)
08-05 19:07:36.617: E/AndroidRuntime(772): Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
08-05 19:07:36.617: E/AndroidRuntime(772):  at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4746)
08-05 19:07:36.617: E/AndroidRuntime(772):  at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:823)
08-05 19:07:36.617: E/AndroidRuntime(772):  at android.view.View.requestLayout(View.java:15473)
08-05 19:07:36.617: E/AndroidRuntime(772):  at android.view.ViewGroup.addView(ViewGroup.java:3208)
08-05 19:07:36.617: E/AndroidRuntime(772):  at android.view.ViewGroup.addView(ViewGroup.java:3186)
08-05 19:07:36.617: E/AndroidRuntime(772):  at com.android.internal.policy.impl.PhoneWindow.generateLayout(PhoneWindow.java:2813)
08-05 19:07:36.617: E/AndroidRuntime(772):  at com.android.internal.policy.impl.PhoneWindow.installDecor(PhoneWindow.java:2875)
08-05 19:07:36.617: E/AndroidRuntime(772):  at com.android.internal.policy.impl.PhoneWindow.getDecorView(PhoneWindow.java:1568)
08-05 19:07:36.617: E/AndroidRuntime(772):  at android.view.Window.findViewById(Window.java:900)
08-05 19:07:36.617: E/AndroidRuntime(772):  at android.app.Activity.findViewById(Activity.java:1839)
08-05 19:07:36.617: E/AndroidRuntime(772):  at com.theproblemsolver.ListView$LoadAllData.doInBackground(ListView.java:90)
08-05 19:07:36.617: E/AndroidRuntime(772):  at com.theproblemsolver.ListView$LoadAllData.doInBackground(ListView.java:1)
08-05 19:07:36.617: E/AndroidRuntime(772):  at android.os.AsyncTask$2.call(AsyncTask.java:287)
08-05 19:07:36.617: E/AndroidRuntime(772):  at java.util.concurrent.FutureTask.run(FutureTask.java:234)
08-05 19:07:36.617: E/AndroidRuntime(772):  ... 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) {

            //} catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                //e.printStackTrace();
            //}
            Intent i = new Intent(MainActivity.this, ListView.class);
            startActivity(i);
        }


        });
    } 
}

列表视图活动:

public class ListView extends ListActivity {

    private ProgressDialog pDialog;

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





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

     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();
            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) {

            EditText et = (EditText) findViewById(R.id.editText1);
            String searchTerm = et.getText().toString().trim();
            try {
                String query = URLEncoder.encode(searchTerm, "utf-8");
                String URL = "http://example.com/json";
                JSONObject jObj = jParser.readJSONFeed(URL);
                try {
                    results = jObj.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

                    }




                });

            }       
}}

JSONParsser 类:

public class JSONParsser {

    InputStream is;
    JSONObject jObj;
    String json = "";
    public EditText et;

    public JSONParsser () {
    }

    public JSONObject readJSONFeed(String URL) {

        try{
        HttpClient client = new DefaultHttpClient();
        HttpPost request = new HttpPost(URL);
        //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();
        }
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            json = sb.toString();
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }

        try {
            jObj = new JSONObject(json);
        } catch (JSONException e) {
           Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        Log.d("JSON String",json);

        return jObj;

        }finally{}

    }{
    }}
4

3 回答 3

0

您无法访问异步任务中的任何布局项。您在 doInBackground 中为您的 EditText 调用 findViewById。为了解决这个问题,您可以将 EditText 和 searchTerm 放在 AsyncTask 之外,并将 searchTerm 设置为最终全局变量,然后您就可以在 AsyncTask 内部访问该变量。

于 2013-08-05T19:47:32.437 回答
0

永远不要从免费线程更新 UI。

请参阅异步任务

当一个异步任务被执行时,任务会经过 4 个步骤:

onPreExecute(),在任务执行之前在 UI 线程上调用。此步骤通常用于设置任务,例如通过在用户界面中显示进度条。

doInBackground(Params...),在 onPreExecute() 完成执行后立即在后台线程上调用。此步骤用于执行可能需要很长时间的后台计算。异步任务的参数传递到这一步。计算的结果必须由这一步返回,并将传递回最后一步。此步骤还可以使用 publishProgress(Progress...) 来发布一个或多个进度单位。这些值在 UI 线程上的 onProgressUpdate(Progress...) 步骤中发布。

onProgressUpdate(Progress...),在调用 publishProgress(Progress...) 后在 UI 线程上调用。执行的时间是不确定的。此方法用于在后台计算仍在执行时在用户界面中显示任何形式的进度。例如,它可用于动画进度条或在文本字段中显示日志。

onPostExecute(Result),在后台计算完成后在 UI 线程上调用。后台计算的结果作为参数传递给该步骤。

于 2013-08-05T19:56:59.093 回答
0

移动这条线

EditText et = (EditText) findViewById(R.id.editText1);

doInBackground()AsyncTaskonCreate()

public class MainActivity extends Activity {

EditText et;
Button getanswer;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    EditText et = (EditText) findViewById(R.id.editText1);  //Here

    Button getanswer = (Button) findViewById(R.id.button1);

UI无法Thread在运行的背景上更新,doInBackground()因此您需要EditTextAsyncTask

于 2013-08-05T19:48:50.843 回答