Java 主线程已挂起,运行时异常

Java 主线程已挂起,运行时异常,java,android,debugging,runtime,Java,Android,Debugging,Runtime,当我运行代码时,所有的错误都会开始抛出。我不知道问题可能是什么,因为理论上我被告知它应该可以工作,eclipse似乎没有检测到任何编码错误。任何关于如何改进代码或如何解决运行时错误的帮助都将不胜感激 代码如下: public class DatabaseActivity extends Activity { /** Called when the activity is first created. */ public DBAdapter DBAdapter =new DBAdapter(th

当我运行代码时,所有的错误都会开始抛出。我不知道问题可能是什么,因为理论上我被告知它应该可以工作,eclipse似乎没有检测到任何编码错误。任何关于如何改进代码或如何解决运行时错误的帮助都将不胜感激

代码如下:

public class DatabaseActivity extends Activity {
/** Called when the activity is first created. */

public DBAdapter
DBAdapter =new DBAdapter(this);

TextView txt;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Create a crude view - this should really be set via the layout resources  
// but since its an example saves declaring them in the XML.  
LinearLayout rootLayout = new LinearLayout(getApplicationContext());  
txt = new TextView(getApplicationContext());  
rootLayout.addView(txt);  
setContentView(rootLayout);  

// Set the text and call the connect function.  
txt.setText("Connecting..."); 
//call the method to run the data retreival
txt.setText(getServerData(KEY_153)); 

}

public static final String KEY_153 = "http://xxxx.xxxx.com/api/execute.php";


private String getServerData(String returnString) {

InputStream is = null;

String result = "";
//the train line to send
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("gender","M"));

//http post
try{
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(KEY_153);
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();

}catch(Exception e){
        Log.e("log_tag", "Error in http connection "+e.toString());
}

//convert response to string
try{
        BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
        }
        is.close();
        result=sb.toString();
}catch(Exception e){
        Log.e("log_tag", "Error converting result "+e.toString());
}

//parse json data
try{
        JSONArray jArray = new JSONArray(result);
        for(int i=0;i<jArray.length();i++){
                JSONObject json_data = jArray.getJSONObject(i);
                DBAdapter.insertPerson(json_data.getString("id"),
                                json_data.getString("firstname"),
                                json_data.getString("surname"),
                                json_data.getString("gender"),
                                json_data.getString("age"),
                                json_data.getString("race"),
                                json_data.getString("height"),
                                json_data.getString("weight"));

                //Get an output to the screen
                returnString += "\n\t" + jArray.getJSONObject(i); 
        }
}catch(JSONException e){
        Log.e("log_tag", "Error parsing data "+e.toString());
}
return returnString; 
}    

}
错误日志:

Database [Android Application]  
DalvikVM[localhost:9115]    
    Thread [<1> main] (Suspended (exception RuntimeException))  
        ActivityThread.performLaunchActivity(ActivityThread$ActivityClientRecord, Intent) line: 1680    
        ActivityThread.handleLaunchActivity(ActivityThread$ActivityClientRecord, Intent) line: 1784 
        ActivityThread.access$1500(ActivityThread, ActivityThread$ActivityClientRecord, Intent) line: 123   
        ActivityThread$H.handleMessage(Message) line: 939   
        ActivityThread$H(Handler).dispatchMessage(Message) line: 99 
        Looper.loop() line: 130 
        ActivityThread.main(String[]) line: 3835    
        Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]  
        Method.invoke(Object, Object...) line: 507  
        ZygoteInit$MethodAndArgsCaller.run() line: 847  
        ZygoteInit.main(String[]) line: 605 
        NativeStart.main(String[]) line: not available [native method]  
    Thread [<8> Binder Thread #2] (Running) 
    Thread [<7> Binder Thread #1] (Running) 
数据库[Android应用程序]
DalvikVM[localhost:9115]
线程[main](挂起(异常运行时异常))
ActivityThread.performLaunchActivity(ActivityThread$ActivityClientRecord,Intent)行:1680
ActivityThread.handleLaunchActivity(ActivityThread$ActivityClientRecord,Intent)行:1784
ActivityThread.access$1500(ActivityThread,ActivityThread$ActivityClientRecord,Intent)行:123
ActivityThread$H.handleMessage(消息)行:939
ActivityThread$H(处理程序)。dispatchMessage(消息)行:99
Looper.loop()行:130
ActivityThread.main(字符串[])行:3835
invokenactive(Object,Object[],Class,Class[],Class,int,boolean)行:不可用[本机方法]
调用(对象,对象…)行:507
ZygoteInit$MethodAndArgsCaller.run()行:847
颧骨单位。主(字符串[])行:605
NativeStart.main(字符串[])行:不可用[本机方法]
螺纹[活页夹螺纹#2](运行)
螺纹[活页夹螺纹#1](运行)

以上大部分内容表明,无法使用debug找到源代码。

尝试设置一些消息框,以便找到运行时错误的位置。我认为
JSONArray jArray=newjsonarray(result)的初始化有问题可能结果无效

您正在主线程上执行网络请求,这将导致ANR&您正在为JSON中的每个人写入持久存储,这可能会导致一个长操作。网络请求应该在一个单独的线程上完成,您应该能够将这些人编译成一个游标,一次写入数据库。

请将代码发布到
ZygoteInit$MethodAndArgsCaller.run()
method当我打开它时,它会告诉我“找不到源代码”并且我应该“编辑源代码查找路径”.我只是在远离主线程的地方设置了一个单独的项目来测试代码。我计划将其实现到主代码中,作为一种服务,稍后我会弄清楚。我打算让主线程承载网络请求。我将如何编写光标的代码?消息框是什么意思?我只是想指出,这段代码昨天起作用了。然后我添加到它,它开始崩溃并显示bug。因此,我将上面所示的代码重写为以前的状态,现在似乎不起作用。
Database [Android Application]  
DalvikVM[localhost:9115]    
    Thread [<1> main] (Suspended (exception RuntimeException))  
        ActivityThread.performLaunchActivity(ActivityThread$ActivityClientRecord, Intent) line: 1680    
        ActivityThread.handleLaunchActivity(ActivityThread$ActivityClientRecord, Intent) line: 1784 
        ActivityThread.access$1500(ActivityThread, ActivityThread$ActivityClientRecord, Intent) line: 123   
        ActivityThread$H.handleMessage(Message) line: 939   
        ActivityThread$H(Handler).dispatchMessage(Message) line: 99 
        Looper.loop() line: 130 
        ActivityThread.main(String[]) line: 3835    
        Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]  
        Method.invoke(Object, Object...) line: 507  
        ZygoteInit$MethodAndArgsCaller.run() line: 847  
        ZygoteInit.main(String[]) line: 605 
        NativeStart.main(String[]) line: not available [native method]  
    Thread [<8> Binder Thread #2] (Running) 
    Thread [<7> Binder Thread #1] (Running)