我正在尝试在我的真实设备上运行一个简单的 android 代码,但它一直在崩溃,它在模拟器上运行良好。
我的代码是
package com.example.new1;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView tx;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tx= (TextView)findViewById(R.id.text);
}
public void func(View view)
{
//tx.setText("Working fine till here.");
new FetchSQL().execute();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
private class FetchSQL extends AsyncTask<String,Void,String>
{
@Override
protected String doInBackground(String... arg0) {
URL url = null;
BufferedReader reader = null;
StringBuilder stringBuilder;
String myUrl = "http://10.22.35.4:80/conc2.php";
try
{ url =new URL(myUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setReadTimeout(15*10000);
connection.connect();
reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
stringBuilder = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
stringBuilder.append(line + "\n");
}
// TODO Auto-generated method stub
return stringBuilder.toString();
}
catch(Exception e)
{
tx.setText(e.toString());
}
return null;
}
protected void onPostExecute(String result)
{
tx.setText(result);
}
}
}
它在模拟器上运行良好,它给出的输出
VM 阿罗拉博士。CP REDDYARTI CHOWDHARYJAGDISH SINGH 但在我的设备上运行相同。它给出了以下错误代码。
05-03 11:49:39.002: E/AndroidRuntime(19934): FATAL EXCEPTION: AsyncTask #1
05-03 11:49:39.002: E/AndroidRuntime(19934): java.lang.RuntimeException: An error occured while executing doInBackground()
05-03 11:49:39.002: E/AndroidRuntime(19934): at android.os.AsyncTask$3.done(AsyncTask.java:200)
05-03 11:49:39.002: E/AndroidRuntime(19934): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
05-03 11:49:39.002: E/AndroidRuntime(19934): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
05-03 11:49:39.002: E/AndroidRuntime(19934): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
05-03 11:49:39.002: E/AndroidRuntime(19934): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
05-03 11:49:39.002: E/AndroidRuntime(19934): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
05-03 11:49:39.002: E/AndroidRuntime(19934): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
05-03 11:49:39.002: E/AndroidRuntime(19934): at java.lang.Thread.run(Thread.java:1096)
05-03 11:49:39.002: E/AndroidRuntime(19934): Caused by: android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
05-03 11:49:39.002: E/AndroidRuntime(19934): at android.view.ViewRoot.checkThread(ViewRoot.java:2811)
05-03 11:49:39.002: E/AndroidRuntime(19934): at android.view.ViewRoot.requestLayout(ViewRoot.java:594)
05-03 11:49:39.002: E/AndroidRuntime(19934): at android.view.View.requestLayout(View.java:8180)
05-03 11:49:39.002: E/AndroidRuntime(19934): at android.view.View.requestLayout(View.java:8180)
05-03 11:49:39.002: E/AndroidRuntime(19934): at android.view.View.requestLayout(View.java:8180)
05-03 11:49:39.002: E/AndroidRuntime(19934): at android.view.View.requestLayout(View.java:8180)
05-03 11:49:39.002: E/AndroidRuntime(19934): at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:257)
05-03 11:49:39.002: E/AndroidRuntime(19934): at android.view.View.requestLayout(View.java:8180)
05-03 11:49:39.002: E/AndroidRuntime(19934): at android.widget.TextView.checkForRelayout(TextView.java:5383)
05-03 11:49:39.002: E/AndroidRuntime(19934): at android.widget.TextView.setText(TextView.java:2688)
05-03 11:49:39.002: E/AndroidRuntime(19934): at android.widget.TextView.setText(TextView.java:2556)
05-03 11:49:39.002: E/AndroidRuntime(19934): at android.widget.TextView.setText(TextView.java:2531)
05-03 11:49:39.002: E/AndroidRuntime(19934): at com.example.new1.MainActivity$FetchSQL.doInBackground(MainActivity.java:66)
05-03 11:49:39.002: E/AndroidRuntime(19934): at com.example.new1.MainActivity$FetchSQL.doInBackground(MainActivity.java:1)
05-03 11:49:39.002: E/AndroidRuntime(19934): at android.os.AsyncTask$2.call(AsyncTask.java:185)
05-03 11:49:39.002: E/AndroidRuntime(19934): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
05-03 11:49:39.002: E/AndroidRuntime(19934): ... 4 more
请帮忙,如何在我的真实设备上运行这个愚蠢的代码。我得到了我的设备
GTI-9003 , ANDROID 2.2.1 (FROYO).. 在这个应用程序中我放了 MIN SDK ..OF 1.5(CUPCAKE)。