![](/img/trans.png)
[英]My code is correct, my Droid emulator runs, but the App will not run (Eclipse)
[英]My android app runs in eclipse emulator but doesn't run on my device
標題說明,當我嘗試在手機上運行我的應用程序時,出現有關類路徑源的錯誤消息,請提供任何幫助
那是我的代碼,並且可以在AVD上完美運行,即使我導出apk文件也可以在android虛擬設備上運行,但是當我將其安裝在手機設備上時,應用程序卻不幸地停止了運行
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/button1"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="23dp"
android:text="TextView" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="application.android.news2"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="application.android.news2.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
public class MainActivity extends Activity {
public void a(){
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet("http://test002.herobo.com/Index2.php");
HttpResponse response = null;
try {
response = client.execute(request);
} catch (ClientProtocolException e3) {
// TODO Auto-generated catch block
e3.printStackTrace();
} catch (IOException e3) {
// TODO Auto-generated catch block
e3.printStackTrace();
}
String html = "";
InputStream in = null;
try {
in = response.getEntity().getContent();
} catch (IllegalStateException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder str = new StringBuilder();
String line = null;
try {
while((line = reader.readLine()) != null)
{
str.append(line);
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
html = str.toString();
String newStr = html;
newStr = newStr.replace("<", "");
newStr = newStr.replace("!", "");
newStr = newStr.replace("-", "");
newStr = newStr.replace(":", "");
newStr = newStr.replace("/", "");
newStr = newStr.replace(">", "");
newStr = newStr.replace(".", "");
newStr = newStr.replace("=", "");
String s='"'+"";
newStr = newStr.replace(s, "");
String delStr = "Hosting24 Analytics Code script typetextjavascript
srchttpstatshosting24comcountphpscript End Of Analytics Code";
newStr = newStr.replace(delStr, "");
TextView textView2 = (TextView)findViewById(R.id.textView);
textView2.setText(newStr);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
a();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
您正在主線程中訪問網絡,這是不允許的。 閱讀本教程以了解如何為此使用AsyncTask。
我的猜測是您的AVD是Android 3.0之前的版本,並且您的設備是Android 3.0及更高版本,並且您正在運行到NetworkOnMainThreadException
,因為您直接從onCreate()
調用a()
方法並執行網絡操作。
嘗試將網絡代碼移到后台線程或AsyncTask中。
您不應該在MainThread中調用a()方法,請按照以下代碼調用a()方法。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new LoadDataTask ().execute();
}
private class LoadDataTask extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... params) {
try {
return a();
} catch (IOException e) {
e.printStackTrace();
return "";
}
}
@Override
protected void onPostExecute(String result) {
refreshTextView(result);
}
}
public String a() {
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet("http://test002.herobo.com/Index2.php");
HttpResponse response = null;
try {
response = client.execute(request);
} catch (ClientProtocolException e3) {
// TODO Auto-generated catch block
e3.printStackTrace();
} catch (IOException e3) {
// TODO Auto-generated catch block
e3.printStackTrace();
}
String html = "";
InputStream in = null;
try {
in = response.getEntity().getContent();
} catch (IllegalStateException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder str = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
str.append(line);
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return str == null ? "" : str.toString();
}
private void refreshTextView(String newStr) {
newStr = newStr.replace("<", "").replace("!", "").replace("-", "")
.replace(":", "").replace("/", "").replace(">", "")
.replace(".", "").replace("=", "");
String s = '"' + "";
newStr = newStr.replace(s, "");
String delStr = "Hosting24 Analytics Code script typetextjavascriptsrchttpstatshosting24comcountphpscript End Of Analytics Code";
newStr = newStr.replace(delStr, "");
TextView textView2 = (TextView) findViewById(R.id.textView);
textView2.setText(newStr);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.