我希望你们都能在这里帮助我。
基本上我的应用程序遇到了一些问题。我之前关于Stack的问题现在已经全部解决了,感谢所有帮助过我的人。
我现在正在接受NPE,但不知道为什么。
基本上我的代码是:
我遇到的问题是我无法激活初始连接。如果有人能帮助我,我将不胜感激。
Java类:
package com.smarte.smartipcontrol;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
public class IPControl extends Activity {
private Socket socket;
private static final int REDIRECTED_SERVERPORT = 32;
public PrintWriter out;
public BufferedReader in;
public String data;
public Object pd;
//get the message from intent
Intent intent = getIntent();
String actu_ip = intent.getStringExtra(IPEntry.ACTUALSMARTIP);
public void getModel(View view) {
try {
out.println("[m\r\n");
//System.out.print("root\r\n");
while(!in.ready());
String textStatus = readBuffer();
} catch(IOException e) {
e.printStackTrace();
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act_ipcontrol);
try {
new AsyncAction().execute();
} catch(Exception e) {
e.printStackTrace();
}
}
private class AsyncAction extends AsyncTask<String, Void, String> {
protected String doInBackground(String... args) {
try {
InetAddress serverAddr = InetAddress.getByName(actu_ip);
socket = new Socket(serverAddr, REDIRECTED_SERVERPORT);
OutputStreamWriter osw = new OutputStreamWriter(socket.getOutputStream());
BufferedWriter bw = new BufferedWriter(osw);
out = new PrintWriter(bw, true);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
while (! in .ready());
readBuffer();
out.println("root\r\n");
while (! in .ready());
readBuffer();
out.println("root\r\n");
while (! in .ready());
String msg = "";
while ( in .ready()) {
msg = msg + (char) in .read();
}
out.println("[c,l#,i5,o*\r\n");
while (! in .ready());
readBuffer();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;//returns what you want to pass to the onPostExecute()
}
protected void onPostExecute(String result) {
//results the data returned from doInbackground
IPControl.this.data = result;
}
}
private String readBuffer() throws IOException {
String msg = "";
while(in.ready()) {
msg = msg + (char)in.read();
}
//System.out.print(msg);
if(msg.indexOf("SNX_COM> ") != -1) return msg.substring(0, msg.indexOf("SNX_COM> "));
else if(msg.indexOf("SCX_COM> ") != -1) return msg.substring(0, msg.indexOf("SCX_COM> "));
else return msg;
}
}
logcat的:
12-07 09:29:24.596: E/AndroidRuntime(772): FATAL EXCEPTION: main
12-07 09:29:24.596: E/AndroidRuntime(772): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.smarte.smartipcontrol/com.smarte.smartipcontrol.IPControl}: java.lang.NullPointerException
12-07 09:29:24.596: E/AndroidRuntime(772): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
12-07 09:29:24.596: E/AndroidRuntime(772): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
12-07 09:29:24.596: E/AndroidRuntime(772): at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-07 09:29:24.596: E/AndroidRuntime(772): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
12-07 09:29:24.596: E/AndroidRuntime(772): at android.os.Handler.dispatchMessage(Handler.java:99)
12-07 09:29:24.596: E/AndroidRuntime(772): at android.os.Looper.loop(Looper.java:137)
12-07 09:29:24.596: E/AndroidRuntime(772): at android.app.ActivityThread.main(ActivityThread.java:5039)
12-07 09:29:24.596: E/AndroidRuntime(772): at java.lang.reflect.Method.invokeNative(Native Method)
12-07 09:29:24.596: E/AndroidRuntime(772): at java.lang.reflect.Method.invoke(Method.java:511)
12-07 09:29:24.596: E/AndroidRuntime(772): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-07 09:29:24.596: E/AndroidRuntime(772): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-07 09:29:24.596: E/AndroidRuntime(772): at dalvik.system.NativeStart.main(Native Method)
12-07 09:29:24.596: E/AndroidRuntime(772): Caused by: java.lang.NullPointerException
12-07 09:29:24.596: E/AndroidRuntime(772): at com.smarte.smartipcontrol.IPControl.<init>(IPControl.java:29)
12-07 09:29:24.596: E/AndroidRuntime(772): at java.lang.Class.newInstanceImpl(Native Method)
12-07 09:29:24.596: E/AndroidRuntime(772): at java.lang.Class.newInstance(Class.java:1319)
12-07 09:29:24.596: E/AndroidRuntime(772): at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
12-07 09:29:24.596: E/AndroidRuntime(772): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
12-07 09:29:24.596: E/AndroidRuntime(772): ... 11 more
先谢谢了。
答案 0 :(得分:1)
阻止您的应用启动的问题包括以下两行:
Intent intent = getIntent();
String actu_ip = intent.getStringExtra(IPEntry.ACTUALSMARTIP);
我无法在初始化中使用getIntent()
,您应该将其移至onCreate()
:
Intent intent;
String actu_ip;
@Override
public void onCreate(Bundle savedInstanceState) {
....
Intent intent = getIntent();
String actu_ip = intent.getStringExtra(IPEntry.ACTUALSMARTIP);
....
}
问候。