[英]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.