应用程序强制在启动时关闭
Posted
技术标签:
【中文标题】应用程序强制在启动时关闭【英文标题】:Application Force Closes On Startup 【发布时间】:2012-04-30 13:03:43 【问题描述】:好吧,我正在尝试制作一个与我已经设置、测试并在一个简单的 java 应用程序后台运行的套接字通信的应用程序。
问题是,我的应用程序会立即强制关闭。在 fcs 之前你没有机会看到任何东西。
这是我的代码:
import java.io.IOException;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Scanner;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;
public class LogMeInClientActivity extends Activity
private Socket s;
private InetAddress hostIp, localIp;
private final String hostName = "localhost";
private final int port = 4000;
private Scanner in;
private PrintWriter out;
private final EditText usernameField = (EditText) findViewById(R.id.username_field),
passwordField = (EditText) findViewById(R.id.password_field);
private String username, password;
@Override
public void onCreate(Bundle savedInstanceState)
System.out.println("A");
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
System.out.println("B");
try
hostIp = InetAddress.getByName(hostName);
localIp = InetAddress.getLocalHost();
System.out.println("Local Ip: " + localIp.toString());
System.out.println("Host Ip: " + hostIp.toString());
catch (UnknownHostException e)
e.printStackTrace();
try
s = new Socket(hostIp, port);
out = new PrintWriter(s.getOutputStream(), true);
in = new Scanner(s.getInputStream());
catch (IOException e)
e.printStackTrace();
private class SignInButtonListener implements OnClickListener
public void onClick(View clickedButton)
switch (clickedButton.getId())
case R.id.sign_in:
username = usernameField.getText().toString();
password = passwordField.getText().toString();
if (username.length() != 0 && password.length() != 0)
out.println(username);
out.println(password);
out.flush();
break;
default:
break;
在上面的代码中,“A”或“B”都不会打印出来。该应用程序只是 fc 的。
这是我的清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.chriswins2much.Util"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".LogMeInClientActivity"
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>
我使用了上述权限,因为我听说它会有所帮助。
我更新了代码以修复 EditText NullPointerException 修复(仅更改部分):
private EditText usernameField, passwordField;
public void onCreate(Bundle savedInstanceState)
System.out.println("A");
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
usernameField = (EditText) findViewById(R.id.username_field);
passwordField = (EditText) findViewById(R.id.password_field);
System.out.println("B");
try
// It's saying one of the errors are on line 37 (directly below)
hostIp = InetAddress.getByName(hostName);
localIp = InetAddress.getLocalHost();
System.out.println("Local Ip: " + localIp.toString());
System.out.println("Host Ip: " + hostIp.toString());
catch (UnknownHostException e)
e.printStackTrace();
try
s = new Socket(hostIp, port);
out = new PrintWriter(s.getOutputStream(), true);
in = new Scanner(s.getInputStream());
catch (IOException e)
e.printStackTrace();
下面是新的logcat:
04-19 02:46:56.292: I/dalvikvm(1289): threadid=3: reacting to signal 3
04-19 02:46:57.049: I/dalvikvm(1289): Wrote stack traces to '/data/anr/traces.txt'
04-19 02:46:57.103: I/dalvikvm(1289): threadid=3: reacting to signal 3
04-19 02:46:57.450: E/dalvikvm(1289): Unable to open stack trace file '/data/anr/traces.txt': Permission denied
04-19 02:46:57.462: I/dalvikvm(1289): threadid=3: reacting to signal 3
04-19 02:46:57.669: I/dalvikvm(1289): Wrote stack traces to '/data/anr/traces.txt'
04-19 02:46:58.069: I/dalvikvm(1289): threadid=3: reacting to signal 3
04-19 02:46:58.409: I/dalvikvm(1289): Wrote stack traces to '/data/anr/traces.txt'
04-19 02:46:58.650: I/dalvikvm(1289): threadid=3: reacting to signal 3
04-19 02:46:58.779: I/dalvikvm(1289): Wrote stack traces to '/data/anr/traces.txt'
04-19 02:46:59.009: I/System.out(1289): A
04-19 02:46:59.153: I/dalvikvm(1289): threadid=3: reacting to signal 3
04-19 02:46:59.321: I/dalvikvm(1289): Wrote stack traces to '/data/anr/traces.txt'
04-19 02:46:59.639: I/dalvikvm(1289): threadid=3: reacting to signal 3
04-19 02:46:59.879: I/dalvikvm(1289): Wrote stack traces to '/data/anr/traces.txt'
04-19 02:47:00.249: I/dalvikvm(1289): threadid=3: reacting to signal 3
04-19 02:47:00.490: I/dalvikvm(1289): Wrote stack traces to '/data/anr/traces.txt'
04-19 02:47:00.819: I/dalvikvm(1289): threadid=3: reacting to signal 3
04-19 02:47:00.970: I/dalvikvm(1289): Wrote stack traces to '/data/anr/traces.txt'
04-19 02:47:01.279: I/dalvikvm(1289): threadid=3: reacting to signal 3
04-19 02:47:01.477: I/dalvikvm(1289): Wrote stack traces to '/data/anr/traces.txt'
04-19 02:47:01.791: I/dalvikvm(1289): threadid=3: reacting to signal 3
04-19 02:47:01.919: I/dalvikvm(1289): Wrote stack traces to '/data/anr/traces.txt'
04-19 02:47:02.302: I/dalvikvm(1289): threadid=3: reacting to signal 3
04-19 02:47:02.443: I/dalvikvm(1289): Wrote stack traces to '/data/anr/traces.txt'
04-19 02:47:02.811: I/dalvikvm(1289): threadid=3: reacting to signal 3
04-19 02:47:02.999: I/dalvikvm(1289): Wrote stack traces to '/data/anr/traces.txt'
04-19 02:47:03.409: I/dalvikvm(1289): threadid=3: reacting to signal 3
04-19 02:47:03.583: I/dalvikvm(1289): Wrote stack traces to '/data/anr/traces.txt'
04-19 02:47:03.870: I/dalvikvm(1289): threadid=3: reacting to signal 3
04-19 02:47:04.029: I/dalvikvm(1289): Wrote stack traces to '/data/anr/traces.txt'
04-19 02:47:04.410: I/dalvikvm(1289): threadid=3: reacting to signal 3
04-19 02:47:04.529: I/System.out(1289): B
04-19 02:47:04.603: I/dalvikvm(1289): Wrote stack traces to '/data/anr/traces.txt'
04-19 02:47:04.850: I/dalvikvm(1289): threadid=3: reacting to signal 3
04-19 02:47:05.020: D/AndroidRuntime(1289): Shutting down VM
04-19 02:47:05.020: W/dalvikvm(1289): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
04-19 02:47:05.101: I/dalvikvm(1289): Wrote stack traces to '/data/anr/traces.txt'
04-19 02:47:05.460: I/dalvikvm(1289): threadid=3: reacting to signal 3
04-19 02:47:05.510: E/AndroidRuntime(1289): FATAL EXCEPTION: main
04-19 02:47:05.510: E/AndroidRuntime(1289): java.lang.RuntimeException: Unable to start activity ComponentInfocom.chriswins2much.Util/com.chriswins2much.Util.LogMeInClientActivity: android.os.NetworkOnMainThreadException
04-19 02:47:05.510: E/AndroidRuntime(1289): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
04-19 02:47:05.510: E/AndroidRuntime(1289): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
04-19 02:47:05.510: E/AndroidRuntime(1289): at android.app.ActivityThread.access$600(ActivityThread.java:123)
04-19 02:47:05.510: E/AndroidRuntime(1289): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
04-19 02:47:05.510: E/AndroidRuntime(1289): at android.os.Handler.dispatchMessage(Handler.java:99)
04-19 02:47:05.510: E/AndroidRuntime(1289): at android.os.Looper.loop(Looper.java:137)
04-19 02:47:05.510: E/AndroidRuntime(1289): at android.app.ActivityThread.main(ActivityThread.java:4424)
04-19 02:47:05.510: E/AndroidRuntime(1289): at java.lang.reflect.Method.invokeNative(Native Method)
04-19 02:47:05.510: E/AndroidRuntime(1289): at java.lang.reflect.Method.invoke(Method.java:511)
04-19 02:47:05.510: E/AndroidRuntime(1289): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-19 02:47:05.510: E/AndroidRuntime(1289): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-19 02:47:05.510: E/AndroidRuntime(1289): at dalvik.system.NativeStart.main(Native Method)
04-19 02:47:05.510: E/AndroidRuntime(1289): Caused by: android.os.NetworkOnMainThreadException
04-19 02:47:05.510: E/AndroidRuntime(1289): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
04-19 02:47:05.510: E/AndroidRuntime(1289): at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
04-19 02:47:05.510: E/AndroidRuntime(1289): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
04-19 02:47:05.510: E/AndroidRuntime(1289): at java.net.InetAddress.getByName(InetAddress.java:295)
04-19 02:47:05.510: E/AndroidRuntime(1289): at com.chriswins2much.Util.LogMeInClientActivity.onCreate(LogMeInClientActivity.java:37)
04-19 02:47:05.510: E/AndroidRuntime(1289): at android.app.Activity.performCreate(Activity.java:4465)
04-19 02:47:05.510: E/AndroidRuntime(1289): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
04-19 02:47:05.510: E/AndroidRuntime(1289): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
04-19 02:47:05.510: E/AndroidRuntime(1289): ... 11 more
04-19 02:47:05.779: I/dalvikvm(1289): Wrote stack traces to '/data/anr/traces.txt'
04-19 02:47:06.839: I/dalvikvm(1289): threadid=3: reacting to signal 3
04-19 02:47:07.000: I/dalvikvm(1289): Wrote stack traces to '/data/anr/traces.txt'
! 注意:这是一个新的、新的更新代码。强制关闭已修复!!!谢谢白痴! 不过还是有问题。它没有连接到我在 java 中在 localhost:4000 上设置的端口。我已经尝试过非Android,它工作正常。这是我修复强制关闭的代码:
public void onCreate(View v)
// Code...
new Thread(new Runnable()
public void run()
try
hostIp = InetAddress.getByName(hostName);
localIp = InetAddress.getLocalHost();
System.out.println("Local Ip: " + localIp.toString());
System.out.println("Host Ip: " + hostIp.toString());
catch (UnknownHostException e)
e.printStackTrace();
try
s = new Socket(hostIp, port);
out = new PrintWriter(s.getOutputStream(), true);
in = new Scanner(s.getInputStream());
catch (IOException e)
e.printStackTrace();
).start();
新logcat如下(后半部分):
04-19 03:38:02.920: I/System.out(1490): Local Ip: localhost/127.0.0.1
04-19 03:38:03.099: I/System.out(1490): Host Ip: localhost/127.0.0.1
04-19 03:38:05.420: W/System.err(1490): java.net.ConnectException: failed to connect to localhost/127.0.0.1 (port 4000): connect failed: ECONNREFUSED (Connection refused)
04-19 03:38:05.711: W/System.err(1490): at libcore.io.IoBridge.connect(IoBridge.java:114)
04-19 03:38:05.711: W/System.err(1490): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
04-19 03:38:05.781: W/System.err(1490): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
04-19 03:38:05.781: W/System.err(1490): at java.net.Socket.startupSocket(Socket.java:566)
04-19 03:38:05.803: W/System.err(1490): at java.net.Socket.<init>(Socket.java:225)
04-19 03:38:05.809: W/System.err(1490): at com.chriswins2much.Util.LogMeInClientActivity$1.run(LogMeInClientActivity.java:48)
04-19 03:38:05.839: W/System.err(1490): at java.lang.Thread.run(Thread.java:856)
04-19 03:38:05.859: W/System.err(1490): Caused by: libcore.io.ErrnoException: connect failed: ECONNREFUSED (Connection refused)
04-19 03:38:05.925: W/System.err(1490): at libcore.io.Posix.connect(Native Method)
04-19 03:38:05.939: W/System.err(1490): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85)
04-19 03:38:05.979: W/System.err(1490): at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
04-19 03:38:05.979: W/System.err(1490): at libcore.io.IoBridge.connect(IoBridge.java:112)
04-19 03:38:05.989: W/System.err(1490): ... 6 more
04-19 03:38:06.692: D/gralloc_goldfish(1490): Emulator without GPU emulation detected.
【问题讨论】:
此行导致错误:passwordField = (EditText) findViewById(R.id.password_field);了解如何阅读堆栈跟踪,它会告诉你这一点(即“Caused by:”) 它仍然无法正常工作,但是,是的,我以前没有看到。提前感谢未来的问题,它可能会救我。 正在使用平板电脑应用程序 NetworkOnMainThread 将发生异常。尝试从另一个非 ui 线程区域调用。尝试访问 ***.com/questions/6343166/… 并在代码中添加严格模式。 对于严格模式,请阅读此developer.android.com/reference/android/os/…。当您必须通过 ui 线程启动线程时,此模式很有用。 喜欢这个? StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().permitAll().build()); 【参考方案1】:好的...显然这是“本地主机”的事情。 Android 运行在另一个子网上并为自己保留 127.0.0.1,所以当我尝试连接到我在 localhost:4000 上设置的服务器时,我调用的是 Android 保留的 localhost ip 127.0.0.1,而计算机的 localhost ip 更改为10.0.2.2 在 4000 端口上。
修复已实施?很简单:
我改变了这个
private final String hostName = "localhost";
到这里
private final String hostName = "10.0.2.2";
或者,如果我尝试使用我的 IP 地址(不是本地主机),那么它应该也可以工作。
【讨论】:
【参考方案2】:我认为您正在使用与互联网相关的东西。我没有在你的清单文件中看到许可。
<uses-permission android:name="android.permissions.INTERNET" />
【讨论】:
我将它添加到清单中,但它仍然给了我同样的 logcat 东西。我的意思完全一样。 我认为是第 37 行的事情,但我不确定 抱歉。它的权限不是权限 hostIp = InetAddress.getByName(hostName);其中主机名 = “本地主机”? 你是从这里粘贴的吗?如果是这样,请将其更改为许可。没有权限【参考方案3】:这些代码可能有问题:
private final EditText usernameField = (EditText) findViewById(R.id.username_field),
passwordField = (EditText) findViewById(R.id.password_field);
视图需要在setContentView
之后初始化,所以改成这样:
private EditText usernameField,passwordField;
在setContentView
之后添加关注代码:
usernameField = (EditText) findViewById(R.id.username_field),
passwordField = (EditText) findViewById(R.id.password_field);
如果它不能解决您的问题,您需要检查视图 ID:R.id.username_field
和 R.id.password_field
是否存在于您的 main.xml
文件中。
更新:
你的新错误:NetworkOnMainThreadException
这仅适用于面向 Honeycomb SDK 或更高版本的应用程序。允许以早期 SDK 版本为目标的应用程序在其主事件循环线程上进行网络连接,但非常不鼓励这样做。请参阅文档响应性设计。
它喜欢你InetAddress.getByName
需要阻止main thread
,所以尝试在另一个线程中使用,或者将sdk目标版本更改为低如:
另外,您需要在 xml 中添加网络权限:
<uses-permission android:name="android.permission.INTERNET" />
【讨论】:
你能用新代码更新并粘贴新的错误信息吗?It's not connecting to the port I have set up on localhost:4000 in java. I've tried it non-Android and it works fine.
你是怎么测试的?问题是:你的安卓设备中有没有4000端口的服务器? SocketSever
启动了吗?如果不是套接字,则需要使用 urlconnection 进行连接。
我在 Java 的 4000 端口上设置了一个 ServerSocket,它与模拟器设置在同一台计算机上。现在,它只是创建了一个新的自定义线程来读取输入并回显它。我在另一个 java 程序中设置了一个 Socket,在 localhost 端口 4000 上设置,该程序使用 Scanner(System.In) 对象从键盘获取输入并发送它。我在 Eclipse 的控制台中同时使用它们。它们可以很好地相互发送和回显。【参考方案4】:
你的问题是 AndroidManifest.xml
如果您将uses-sdk android:minSdkVersion="15" 更改为 uses-sdk android:minSdkVersion="8" 程序没问题。
我不知道怎么办
【讨论】:
以上是关于应用程序强制在启动时关闭的主要内容,如果未能解决你的问题,请参考以下文章
当设备重新启动并强制关闭应用程序时,Redmi 和其他自定义中文 ROM 中未收到 WorkManager doWork 回调