0

我想用来自 json 对象的值更改我在 android 应用程序中的单选按钮文本... q_desc textView 更改...但不是单选按钮的文本...更改 q_desc 文本视图强制关闭后的应用程序....我的错误是什么..??? 我提供了我的 jsp 页面、活动以及我的日志猫..

这是我的jsp代码....

<%@page contentType="text/html; charset=UTF-8"%>
<%@page import="org.json.simple.JSONObject"%>
<%@page import="java.util.*,java.sql.*"%>

 <%!
 Connection con;
 PreparedStatement ps;
 ResultSet rs;
 String x;

 %>

<%
 Class.forName("oracle.jdbc.driver.OracleDriver");
 con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","pro","pro");
 ps=con.prepareStatement("select * from c_question where id=1");
 rs=ps.executeQuery();

JSONObject obj=new JSONObject();
obj.put("id",rs.getString(1));
obj.put("desc",rs.getString(2));
obj.put("a",rs.getString(3));
obj.put("b",rs.getString(4));
obj.put("c",rs.getString(5));
obj.put("d",rs.getString(6));

out.print(obj);
out.flush();
 %>

这是我的活动...

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class JsonDemo extends Activity 
{
JSONObject json;
HttpClient client;
TextView q_desc;
RadioButton rb_a;
RadioButton rb_b;
RadioButton rb_c;
RadioButton rb_d;
RadioGroup rg;


final static String URL="http://10.0.2.2:7001/project/json.jsp";

@Override
protected void onCreate(Bundle savedInstanceState) {

    // TODO Auto-generated method stub

    super.onCreate(savedInstanceState);

    setContentView(R.layout.result_json);

    q_desc=(TextView)findViewById(R.id.q_desc);
        rb_a=(RadioButton)findViewById(R.id.opt_a);
        rb_b=(RadioButton)findViewById(R.id.opt_b);
        rb_c=(RadioButton)findViewById(R.id.opt_c);
        rb_d=(RadioButton)findViewById(R.id.opt_d);

        client=new DefaultHttpClient();

       new Read().execute("desc","a","b","c","d");

}
   public  JSONObject getData()throws      ClientProtocolException,IOException,JSONException
   {

    StringBuilder url=new StringBuilder(URL);
    HttpGet get=new HttpGet(url.toString());
    HttpResponse r=client.execute(get);



    int status=r.getStatusLine().getStatusCode();

    if(status == 200)
    {
        HttpEntity e=r.getEntity();
        String data=EntityUtils.toString(e);
        JSONObject last=new JSONObject(data);
        return last;

    }
    else
    {
        Toast.makeText(JsonDemo.this, "error", Toast.LENGTH_SHORT);
        return null;
    }

}



 public class Read extends AsyncTask<String,Integer,String>
 {

@Override
protected String doInBackground(String... params) {
    // TODO Auto-generated method stub
    try {

        json=getData();
        String desc=json.getString(params[0]);
        String option_a=json.getString(params[1]);
        String option_b=json.getString(params[2]);
        String option_c=json.getString(params[3]);
        String option_d=json.getString(params[4]);
        set_ques(desc);
        set_options(option_a,option_b,option_c,option_d);

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

protected void set_ques(String desc) {
    // TODO Auto-generated method stub
    q_desc.setText(desc);
}
protected void set_options(String a,String b,String c,String d) {
    // TODO Auto-generated method stub
    rb_a.setText(a);
    rb_b.setText(b);
    rb_c.setText(c);
    rb_d.setText(d);
}


}

这是原木猫……

 04-05 22:17:52.198: W/dalvikvm(445): threadid=7: thread exiting with uncaught exception (group=0x4001d800)
 04-05 22:17:52.968: E/AndroidRuntime(445): FATAL EXCEPTION: AsyncTask #1
 04-05 22:17:52.968: E/AndroidRuntime(445): java.lang.RuntimeException: An error occured while executing doInBackground()
 04-05 22:17:52.968: E/AndroidRuntime(445):     at android.os.AsyncTask$3.done(AsyncTask.java:200)
 04-05 22:17:52.968: E/AndroidRuntime(445):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
 04-05 22:17:52.968: E/AndroidRuntime(445):     at   java.util.concurrent.FutureTask.setException(FutureTask.java:124)
 04-05 22:17:52.968: E/AndroidRuntime(445):     at    java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
 04-05 22:17:52.968: E/AndroidRuntime(445):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
 04-05 22:17:52.968: E/AndroidRuntime(445):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
 04-05 22:17:52.968: E/AndroidRuntime(445):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
 04-05 22:17:52.968: E/AndroidRuntime(445):     at java.lang.Thread.run(Thread.java:1096)
 04-05 22:17:52.968: E/AndroidRuntime(445): Caused by: android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
 04-05 22:17:52.968: E/AndroidRuntime(445):     at android.view.ViewRoot.checkThread(ViewRoot.java:2802)
 04-05 22:17:52.968: E/AndroidRuntime(445):     at android.view.ViewRoot.invalidateChild(ViewRoot.java:607)
 04-05 22:17:52.968: E/AndroidRuntime(445):     at android.view.ViewRoot.invalidateChildInParent(ViewRoot.java:633)
 04-05 22:17:52.968: E/AndroidRuntime(445):     at android.view.ViewGroup.invalidateChild(ViewGroup.java:2505)
 04-05 22:17:52.968: E/AndroidRuntime(445):     at  android.view.View.invalidate(View.java:5139)
 04-05 22:17:52.968: E/AndroidRuntime(445):     at android.widget.TextView.checkForRelayout(TextView.java:5364)
 04-05 22:17:52.968: E/AndroidRuntime(445):     at android.widget.TextView.setText(TextView.java:2688)
 04-05 22:17:52.968: E/AndroidRuntime(445):     at android.widget.TextView.setText(TextView.java:2556)
 04-05 22:17:52.968: E/AndroidRuntime(445):     at android.widget.TextView.setText(TextView.java:2531)
 04-05 22:17:52.968: E/AndroidRuntime(445):     at com.campuspro.start.JsonDemo$Read.set_ques(JsonDemo.java:122)
 04-05 22:17:52.968: E/AndroidRuntime(445):     at com.campuspro.start.JsonDemo$Read.doInBackground(JsonDemo.java:101)
 04-05 22:17:52.968: E/AndroidRuntime(445):     at com.campuspro.start.JsonDemo$Read.doInBackground(JsonDemo.java:1)
 04-05 22:17:52.968: E/AndroidRuntime(445):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
 04-05 22:17:52.968: E/AndroidRuntime(445):     at           java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
 04-05 22:17:52.968: E/AndroidRuntime(445):     ... 4 more
4

1 回答 1

3

写在OnPostExecute(...) 因为doinbackground is non UI thread

所以你不能更新你的视图......在你从 URL 获取数据并完成并最终OnPostExecute被调用后,添加这个......

 String desc=json.getString(params[0]);
        String option_a=json.getString(params[1]);
        String option_b=json.getString(params[2]);
        String option_c=json.getString(params[3]);
        String option_d=json.getString(params[4]);
        set_ques(desc);
        set_options(option_a,option_b,option_c,option_d);
于 2012-04-05T17:01:27.840 回答