0

标题解释说,当我尝试在手机上运行我的应用程序时,eclipse 会给我一条关于类路径源的错误消息,请提供任何帮助

那是我的代码,它在 AVD 上完美运行即使我导出 apk 文件,它也可以在 android 虚拟设备上运行,但是当我将它安装在我的手机设备上时,应用程序不幸停止了

activity_main.xml

<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

<?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>

MainActivity.java

   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;
}

 }
4

3 回答 3

1

您正在主线程中访问网络,这是不允许的。阅读本教程以了解如何为此使用 AsyncTask。

于 2013-08-18T21:48:55.577 回答
0

我的猜测是您的 AVD 是 Android 3.0 之前的版本,并且您的设备是 Android 3.0 及更高版本,并且您正在运行NetworkOnMainThreadException,因为您的a()方法是直接从 调用的onCreate(),并且进行网络操作。

尝试将您的网络代码移动到后台线程或 AsyncTask 中。

于 2013-08-18T21:47:42.430 回答
0

您不应该在 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);
}
于 2013-08-18T22:24:30.413 回答