当前位置: 首页 > 知识库问答 >
问题:

应用程序在android 2.2中运行良好,但在4.2中意外关闭

司徒炎彬
2023-03-14

这款应用在android 2.2中运行得非常好。但在4.2中,该应用程序意外关闭。该应用程序使用客户端服务器架构向充当服务器的笔记本电脑发送消息。logcat显示:

02-07 04:49:45.683: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:45.683: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:45.690: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:46.160: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:46.203: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:47.803: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:47.810: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:48.000: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:48.050: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:48.182: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:48.190: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:48.262: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:48.440: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:48.440: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:48.470: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:48.480: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:48.480: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:48.500: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:48.550: D/gralloc_goldfish(829): Emulator without GPU emulation detected.
02-07 04:49:49.030: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:49.060: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:49.210: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:49.210: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:50.433: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:50.433: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:50.473: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:50.473: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:50.860: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:50.860: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:50.993: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:50.993: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:51.040: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:51.040: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:51.490: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:51.490: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:51.530: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:51.530: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:52.003: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:52.003: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:52.070: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:52.070: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:52.503: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:52.503: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:52.563: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:52.563: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:53.011: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:53.011: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:53.071: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:53.071: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:53.540: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:53.540: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:53.600: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:53.600: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:54.020: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:54.020: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:54.100: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:54.100: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:54.550: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:54.570: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:54.750: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:54.750: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:49:55.070: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:50:20.590: D/dalvikvm(829): GC_CONCURRENT freed 193K, 11% free 2566K/2876K, paused 92ms+42ms, total 1402ms
02-07 04:50:20.590: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:50:20.610: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:50:20.653: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:50:20.660: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:50:20.750: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:50:20.750: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:50:21.284: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:50:21.290: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:50:21.355: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:50:21.360: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:50:21.560: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:50:21.560: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:50:21.610: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:50:21.768: W/Trace(829): Unexpected value from nativeGetEnabledTags: 0
02-07 04:50:22.083: D/AndroidRuntime(829): Shutting down VM
02-07 04:50:22.090: W/dalvikvm(829): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
02-07 04:50:22.190: E/AndroidRuntime(829): FATAL EXCEPTION: main
02-07 04:50:22.190: E/AndroidRuntime(829): android.os.NetworkOnMainThreadException
02-07 04:50:22.190: E/AndroidRuntime(829):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
02-07 04:50:22.190: E/AndroidRuntime(829):  at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
02-07 04:50:22.190: E/AndroidRuntime(829):  at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
02-07 04:50:22.190: E/AndroidRuntime(829):  at libcore.io.IoBridge.connect(IoBridge.java:112)
02-07 04:50:22.190: E/AndroidRuntime(829):  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
02-07 04:50:22.190: E/AndroidRuntime(829):  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
02-07 04:50:22.190: E/AndroidRuntime(829):  at java.net.Socket.startupSocket(Socket.java:566)
02-07 04:50:22.190: E/AndroidRuntime(829):  at java.net.Socket.tryAllAddresses(Socket.java:127)
02-07 04:50:22.190: E/AndroidRuntime(829):  at java.net.Socket.<init>(Socket.java:177)
02-07 04:50:22.190: E/AndroidRuntime(829):  at java.net.Socket.<init>(Socket.java:149)
02-07 04:50:22.190: E/AndroidRuntime(829):  at com.example.sample2.MainActivity$1.onClick(MainActivity.java:39)
02-07 04:50:22.190: E/AndroidRuntime(829):  at     android.view.View.performClick(View.java:4202)
02-07 04:50:22.190: E/AndroidRuntime(829):  at android.view.View$PerformClick.run(View.java:17340)
02-07 04:50:22.190: E/AndroidRuntime(829):  at android.os.Handler.handleCallback(Handler.java:725)
02-07 04:50:22.190: E/AndroidRuntime(829):  at android.os.Handler.dispatchMessage(Handler.java:92)
02-07 04:50:22.190: E/AndroidRuntime(829):  at android.os.Looper.loop(Looper.java:137)
02-07 04:50:22.190: E/AndroidRuntime(829):  at android.app.ActivityThread.main(ActivityThread.java:5039)
02-07 04:50:22.190: E/AndroidRuntime(829):  at java.lang.reflect.Method.invokeNative(Native Method)
02-07 04:50:22.190: E/AndroidRuntime(829):  at java.lang.reflect.Method.invoke(Method.java:511)
02-07 04:50:22.190: E/AndroidRuntime(829):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-07 04:50:22.190: E/AndroidRuntime(829):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-07 04:50:22.190: E/AndroidRuntime(829):  at dalvik.system.NativeStart.main(Native Method)
02-07 04:50:30.530: I/Process(829): Sending signal. PID: 829 SIG: 9

我的客户代码是

public class MainActivity extends Activity {

 private Socket client;
 private PrintWriter printwriter;
 private EditText textField;
 private Button button;
 private String messsage;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    textField = (EditText) findViewById(R.id.editText1);
    button = (Button) findViewById(R.id.button1);
    button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            messsage = textField.getText().toString(); 
            textField.setText("");    
            try {

                client = new Socket("10.0.2.2", 4444);  
                printwriter = new PrintWriter(client.getOutputStream(),true);
                printwriter.write(messsage);  
                printwriter.flush();
                printwriter.close();
                client.close();  
               } catch (UnknownHostException e) {
                e.printStackTrace();
               } catch (IOException e) {
                e.printStackTrace();
               }

        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

}

请帮我解决它。

共有2个答案

淳于健
2023-03-14

您需要将网络活动从主线程中移除。

看看异步任务

华哲茂
2023-03-14

您正在主 UI 线程中执行网络操作。如果你的目标 SDK 是 11(蜂窝)或更高,这将引发网络操作系统异常 ,因为此行为可能会阻止 UI 并导致应用无响应。

您可以使用<code>AsyncTask</code>来绕过此问题,方法是将数据加载到其<code>doInBackground(..)</code>中。

 类似资料:
  • 我是Android开发的新手。我能够使这个应用程序在Android5.1中工作,但是,当我试图在Android4.1中部署这个应用程序时,它在主活动本身就会崩溃。我理解它,因为我使用的是矢量图像和Android4.1不兼容它。为了克服这一点,我设置了如下所述的代码: 我现在收到这个错误 代码文件 XML文件 请帮助我调试这个。我有点被困住了,需要你的帮助。

  • 这里是我的主要活动 } 我感觉runOnUiThread方法有问题。感谢你的帮助.

  • 当我执行时 在控制台(javascript)中,它通过类名返回元素,但是当我执行时: < code > driver . find elements(by . class name(" jobs-company-card _ _ content-wrapper ")); 结果(元素)是一个空的列表,注意返回

  • 我使用kotlin中的EclipsePAHOMQTT库和IntellijIDE中的Gradle创建了一个小型mqtt应用程序。当通过Intellij运行它时,它运行良好,但是当我构建它并运行创建的jar文件时,我会得到一个错误。 从我看到的其他问题来看,这似乎与类路径有关,但我不确定如果这确实是问题所在,需要做什么,因为我使用的是gradle而不是jar文件作为库。 我正在学习这个教程 这是我的档

  • 非常感谢任何帮助。 我试过这个: 我真的被困在这里了。没有看到入口服务显示像我会在minikube和我不知道为什么。

  • 我有应用程序上传在玩商店。它在调试模式下运行良好,但当我从play store下载相同的APK(发布模式)时,它就崩溃了。我无法确定堆栈跟踪没有给出准确的错误位置。 java.lang.RuntimeException:在Android.os.AsyncTask$3上完成(asynctask.java:318)在java.util.concurrent.FutureTask.FinishCompl