我想在我的真實設備上運行簡單的android代碼,但它不斷崩潰,它在仿真器上正常工作。在真實設備上的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);
}
}
}
它可以在模擬器罰款,輸出它給
V. M. ARORADr。 C. P. 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)。
爲什麼所有的帽子?奇怪。 – Raptor 2013-05-03 06:30:34
在'doInBackground()'方法內註釋'tx.setText(e.toString())'行,然後再次發佈logcat輸出。 – TactMayers 2013-05-03 06:43:16
也許你可以用實際日誌更新你的問題。 – Ashwin 2013-05-03 10:33:18