这是我的第一个应用程序之一 - 目前,它应该作为具有HLK-RM04的TCP终端。
我知道此错误已经发布过,但我没有找到适用于我的代码的任何解决方案。帮助将不胜感激。
注意#1:即使连接可用,也会给出相同的错误。
注意#2:如果我删除第77-84行和第98行,如果连接可用则没有错误(如果没有,则应用程序退出)。
似乎有问题的代码:
package com.milk.milkdispenser;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.util.Log;
import android.os.Handler;
import java.util.Timer;
import java.util.TimerTask;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.io.IOException;
public class MainActivity extends Activity {
//Debug string
String msg= "milkMsg";
//Variables
String tmp;
//Declaring buffers...
PrintWriter outp;
BufferedReader inp;
//Objects
EditText txt;
Button send;
Button clear;
TextView output;
//Handler
final Handler MyHandler = new Handler();
//Socket
Socket s;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.d(msg, "Starting app...");
//assigning objects
txt = (EditText) findViewById(R.id.editText1);
send = (Button) findViewById(R.id.button1);
clear = (Button) findViewById(R.id.button2);
output = (TextView) findViewById(R.id.textView1);
//Connecting socket
try{s=new Socket("192.168.11.154",8080);}
catch(IOException err1){
Log.d(msg, "ERROR: Could not connect!");
Log.d(msg, msg, err1.getCause());
Log.d(msg, err1.getMessage());
Toast.makeText(this, "Connection error - restart the app", Toast.LENGTH_LONG).show();
this.finish();
}
//Defining Buffers...
try {
outp = new PrintWriter(s.getOutputStream(), true);
//LINE 78 ABOVE!!!!!!!
inp = new BufferedReader(new InputStreamReader(s.getInputStream()));
tmp = inp.readLine();
} catch (IOException e) {
e.printStackTrace();
}
//Timer routines
MyTimerTask myTask = new MyTimerTask();
Timer myTimer = new Timer();
myTimer.schedule(myTask, 0, 200);
//Sending message
//txt.getText()
send.setOnClickListener(new OnClickListener(){
public void onClick(View v){
Log.d(msg, "Sending text...");
//Sending text...
outp.print(txt.getText().toString()+"\r\n");
txt.setText("");
}
});
clear.setOnClickListener(new OnClickListener(){
public void onClick(View v){
Log.d(msg, "Clearing...");
output.setText("");
}
});
}
class MyTimerTask extends TimerTask{
@Override
public void run(){
MyHandler.post(myRunnable);
}
}
final Runnable myRunnable = new Runnable() {
public void run() {
//Buffer verifying..
}};
protected void onDestroy(){
super.onDestroy();
Log.d(msg, "CLOSING!");
try {
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
LogCat(已更新,因此显示异常):
07-12 03:41:20.520: D/milkMsg(4646): Starting app...
07-12 03:41:20.520: D/milkMsg(4646): ERROR: Could not connect!
07-12 03:41:20.540: D/milkMsg(4646): milkMsg
07-12 03:41:20.540: D/milkMsg(4646): libcore.io.ErrnoException: socket failed: EACCES (Permission denied)
07-12 03:41:20.540: D/milkMsg(4646): at libcore.io.Posix.socket(Native Method)
07-12 03:41:20.540: D/milkMsg(4646): at libcore.io.BlockGuardOs.socket(BlockGuardOs.java:181)
07-12 03:41:20.540: D/milkMsg(4646): at libcore.io.IoBridge.socket(IoBridge.java:568)
07-12 03:41:20.540: D/milkMsg(4646): at java.net.PlainSocketImpl.create(PlainSocketImpl.java:201)
07-12 03:41:20.540: D/milkMsg(4646): at java.net.Socket.startupSocket(Socket.java:559)
07-12 03:41:20.540: D/milkMsg(4646): at java.net.Socket.tryAllAddresses(Socket.java:127)
07-12 03:41:20.540: D/milkMsg(4646): at java.net.Socket.<init>(Socket.java:177)
07-12 03:41:20.540: D/milkMsg(4646): at java.net.Socket.<init>(Socket.java:149)
07-12 03:41:20.540: D/milkMsg(4646): at com.milk.milkdispenser.MainActivity.onCreate(MainActivity.java:66)
07-12 03:41:20.540: D/milkMsg(4646): at android.app.Activity.performCreate(Activity.java:5008)
07-12 03:41:20.540: D/milkMsg(4646): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
07-12 03:41:20.540: D/milkMsg(4646): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
07-12 03:41:20.540: D/milkMsg(4646): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
07-12 03:41:20.540: D/milkMsg(4646): at android.app.ActivityThread.access$600(ActivityThread.java:130)
07-12 03:41:20.540: D/milkMsg(4646): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
07-12 03:41:20.540: D/milkMsg(4646): at android.os.Handler.dispatchMessage(Handler.java:99)
07-12 03:41:20.540: D/milkMsg(4646): at android.os.Looper.loop(Looper.java:137)
07-12 03:41:20.540: D/milkMsg(4646): at android.app.ActivityThread.main(ActivityThread.java:4745)
07-12 03:41:20.540: D/milkMsg(4646): at java.lang.reflect.Method.invokeNative(Native Method)
07-12 03:41:20.540: D/milkMsg(4646): at java.lang.reflect.Method.invoke(Method.java:511)
07-12 03:41:20.540: D/milkMsg(4646): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
07-12 03:41:20.540: D/milkMsg(4646): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-12 03:41:20.540: D/milkMsg(4646): at dalvik.system.NativeStart.main(Native Method)
07-12 03:41:20.540: D/milkMsg(4646): socket failed: EACCES (Permission denied)
07-12 03:41:20.610: D/AndroidRuntime(4646): Shutting down VM
07-12 03:41:20.610: W/dalvikvm(4646): threadid=1: thread exiting with uncaught exception (group=0x40c73300)
07-12 03:41:20.620: E/AndroidRuntime(4646): FATAL EXCEPTION: main
07-12 03:41:20.620: E/AndroidRuntime(4646): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.milk.milkdispenser/com.milk.milkdispenser.MainActivity}: java.lang.NullPointerException
07-12 03:41:20.620: E/AndroidRuntime(4646): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
07-12 03:41:20.620: E/AndroidRuntime(4646): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
07-12 03:41:20.620: E/AndroidRuntime(4646): at android.app.ActivityThread.access$600(ActivityThread.java:130)
07-12 03:41:20.620: E/AndroidRuntime(4646): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
07-12 03:41:20.620: E/AndroidRuntime(4646): at android.os.Handler.dispatchMessage(Handler.java:99)
07-12 03:41:20.620: E/AndroidRuntime(4646): at android.os.Looper.loop(Looper.java:137)
07-12 03:41:20.620: E/AndroidRuntime(4646): at android.app.ActivityThread.main(ActivityThread.java:4745)
07-12 03:41:20.620: E/AndroidRuntime(4646): at java.lang.reflect.Method.invokeNative(Native Method)
07-12 03:41:20.620: E/AndroidRuntime(4646): at java.lang.reflect.Method.invoke(Method.java:511)
07-12 03:41:20.620: E/AndroidRuntime(4646): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
07-12 03:41:20.620: E/AndroidRuntime(4646): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-12 03:41:20.620: E/AndroidRuntime(4646): at dalvik.system.NativeStart.main(Native Method)
07-12 03:41:20.620: E/AndroidRuntime(4646): Caused by: java.lang.NullPointerException
07-12 03:41:20.620: E/AndroidRuntime(4646): at com.milk.milkdispenser.MainActivity.onCreate(MainActivity.java:78)
07-12 03:41:20.620: E/AndroidRuntime(4646): at android.app.Activity.performCreate(Activity.java:5008)
07-12 03:41:20.620: E/AndroidRuntime(4646): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
07-12 03:41:20.620: E/AndroidRuntime(4646): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
07-12 03:41:20.620: E/AndroidRuntime(4646): ... 11 more
答案 0 :(得分:0)
libcore.io.ErrnoException: socket failed: EACCES (Permission denied)
07-12 03:41:20.540:D / milkMsg(4646):at libcore.io.Posix.socket(Native Method)
首先,您错过了互联网许可。添加到您的清单:
<uses-permission android:name="android.permission.INTERNET" />
其次,您正在UI线程上进行网络操作。使用例如将它们移动到后台线程AsyncTask
。见How to fix android.os.NetworkOnMainThreadException?