0

我希望你们都可以在这里帮助我。

基本上我的应用程序遇到了一些问题。我之前在 Stack 上的问题现在都已经解决了,感谢所有帮助我的人。

我现在正在获得 NPE,但不知道为什么。

基本上我的代码是:

  1. 连接到设备(来自意图的 IP 地址和固定端口 32)
  2. 一旦连接发送命令“root/r/n”两次。(这是设备登录而不是实际连接,连接不受保护。
  3. 一旦返回“SNX_COM>”或“SCX_COM>”等待命令。
  4. 然后我希望能够通过单击按钮向他们发送命令。

我遇到的问题是我无法激活初始连接。如果有人可以帮助我,将不胜感激。

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;
}
}

日志猫:

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

先谢谢了。

4

1 回答 1

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);
    ....
}

问候。

于 2012-12-07T10:15:30.067 回答