我的Android应用程序在模拟器中工作正常,但是当我安装.apk并运行时,它说"不幸的是'应用程序'已经停止"
这是androidmanifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.baliweather"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity android:name=".Main"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Prakiraan"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar">
</activity>
<activity android:name=".Update"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar">
</activity>
<activity android:name=".Info"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar">
</activity>
</application>
错误日志:
09-02 15:25:01.372:E / AndroidRuntime(16394):致命异常:主要 09-02 15:25:01.372:E / AndroidRuntime(16394):android.os.NetworkOnMainThreadException 09-02 15:25:01.372:E / AndroidRuntime(16394):在android.os.StrictMode $ AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1133) 09-02 15:25:01.372:E / AndroidRuntime(16394):at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:163) 09-02 15:25:01.372:E / AndroidRuntime(16394):at libcore.io.IoBridge.recvfrom(IoBridge.java:519) 09-02 15:25:01.372:E / AndroidRuntime(16394):at java.net.PlainSocketImpl.read(PlainSocketImpl.java:489) 09-02 15:25:01.372:E / AndroidRuntime(16394):at java.net.PlainSocketImpl.access $ 000(PlainSocketImpl.java:46) 09-02 15:25:01.372:E / AndroidRuntime(16394):at java.net.PlainSocketImpl $ PlainSocketInputStream.read(PlainSocketImpl.java:241) 09-02 15:25:01.372:E / AndroidRuntime(16394):at java.io.InputStream.read(InputStream.java:163) 09-02 15:25:01.372:E / AndroidRuntime(16394):at java.io.BufferedInputStream.fillbuf(BufferedInputStream.java:142) 09-02 15:25:01.372:E / AndroidRuntime(16394):at java.io.BufferedInputStream.read(BufferedInputStream.java:227) 09-02 15:25:01.372:E / AndroidRuntime(16394):at libcore.io.Streams.readAsciiLine(Streams.java:201) 09-02 15:25:01.372:E / AndroidRuntime(16394):at libcore.net.http.HttpEngine.readResponseHeaders(HttpEngine.java:579) 09-02 15:25:01.372:E / AndroidRuntime(16394):at libcore.net.http.HttpEngine.readResponse(HttpEngine.java:827) 09-02 15:25:01.372:E / AndroidRuntime(16394):at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:283) 09-02 15:25:01.372:E / AndroidRuntime(16394):at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177) 09-02 15:25:01.372:E / AndroidRuntime(16394):at java.net.URL.openStream(URL.java:462) 09-02 15:25:01.372:E / AndroidRuntime(16394):at com.android.baliweather.MyParser.xmlParsing(MyParser.java:43) 09-02 15:25:01.372:E / AndroidRuntime(16394):at com.android.baliweather.Prakiraan $ MyAsyncTask.onPostExecute(Prakiraan.java:60) 09-02 15:25:01.372:E / AndroidRuntime(16394):at com.android.baliweather.Prakiraan $ MyAsyncTask.onPostExecute(Prakiraan.java:1) 09-02 15:25:01.372:E / AndroidRuntime(16394):在android.os.AsyncTask.finish(AsyncTask.java:631) 09-02 15:25:01.372:E / AndroidRuntime(16394):在android.os.AsyncTask.access $ 600(AsyncTask.java:177) 09-02 15:25:01.372:E / AndroidRuntime(16394):在android.os.AsyncTask $ InternalHandler.handleMessage(AsyncTask.java:644) 09-02 15:25:01.372:E / AndroidRuntime(16394):在android.os.Handler.dispatchMessage(Handler.java:99) 09-02 15:25:01.372:E / AndroidRuntime(16394):在android.os.Looper.loop(Looper.java:137) 09-02 15:25:01.372:E / AndroidRuntime(16394):在android.app.ActivityThread.main(ActivityThread.java:5214) 09-02 15:25:01.372:E / AndroidRuntime(16394):at java.lang.reflect.Method.invokeNative(Native Method) 09-02 15:25:01.372:E / AndroidRuntime(16394):at java.lang.reflect.Method.invoke(Method.java:525) 09-02 15:25:01.372:E / AndroidRuntime(16394):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:739) 09-02 15:25:01.372:E / AndroidRuntime(16394):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555) 09-02 15:25:01.372:E / AndroidRuntime(16394):at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
android.os.NetworkOnMainThreadException
非常自我解释,对吗?您正在UI线程上执行网络任务(HTTP请求等)。你应该在一个单独的线程中运行它。
private class LongOperation extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
for (int i = 0; i < 5; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Thread.interrupted();
}
}
return "Executed";
}
@Override
protected void onPostExecute(String result) {
TextView txt = (TextView) findViewById(R.id.output);
txt.setText("Executed"); // txt.setText(result);
// might want to change "executed" for the returned string passed
// into onPostExecute() but that is upto you
}
@Override
protected void onPreExecute() {}
@Override
protected void onProgressUpdate(Void... values) {}
}
上面一个例子。
将其作为活动中的内部课程
答案 1 :(得分:0)
将问题上传到playstore
后我遇到同样的问题。
当应用崩溃时,您可以看到两个按钮,强制停止/确定并报告。点击report button
,您会在那里找到各种标签。在其中一个选项卡中,您将获得堆栈跟踪。然后你就可以知道你的错误在哪里......
有一次清理项目并重新创建一个apk然后再检查..