标题解释说,当我尝试在手机上运行我的应用时,eclipse会给我一个关于类路径源的错误消息,请任何帮助
这是我的代码,它在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;
}
}
答案 0 :(得分:1)
您正在主线程中访问网络,这是不允许的。阅读this tutorial以了解如何使用AsyncTask。
答案 1 :(得分:0)
我的猜测是你的AVD是预装Android 3.0,你的设备是Android 3.0及更高版本,并且你遇到了NetworkOnMainThreadException
,因为你a()
方法直接来自onCreate()
{1}},并进行网络操作。
尝试将网络代码移动到后台线程或AsyncTask中。
答案 2 :(得分:0)
你不应该在MainThread中调用()方法,请按照下面的代码调用()方法。
@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);
}