java.lang.RuntimeException:无法启动活动ComponentInfo {}:java.lang.NullPointerException

时间:2014-03-31 08:30:10

标签: android nullpointerexception

java.lang.RuntimeException: 
Unable to start     activity ComponentInfo{org.example.screens/edu.dongthang.controller.Controller}: 
java.lang.NullPointerException

帮助我,在尝试使用代码

在课前打开类“Controller”时出现此错误
public void clickHandler(View view) {
    AppDelegate appDel = ((AppDelegate) getApplicationContext());
    appDel.mouse_sensitivity = Math.round(50 / 20) + 1;
    if (!appDel.connected) {
        String serverIp;
        int serverPort;

        serverIp = ipField.getText().toString();    
        serverPort = Integer.parseInt(portField.getText().toString());
        appDel.createClientThread(serverIp, serverPort);
    }

    int x;
    for (x = 0; x < 4; x++) {// every quarter second for one second check if
                                // the server is reachable
        if (appDel.connected) {
            startActivity(new Intent(view.getContext(), Controller.class));
            x = 6;
        }
        try {
            Thread.sleep(250);
        } catch (Exception e) {
        }
    }

    if (!appDel.connected)
        if (!appDel.network_reachable)
            network_alert.show();
        else
            alert.show();
}

这是类控制器     包edu.dongthang.controller;

public class Controller extends Activity implements OnTouchListener,
    OnKeyListener, SimpleGestureListener{

int lastXpos = 0;
int lastYpos = 0;
static boolean mousemode = false;
boolean keyboard = false;
Thread checking;
SimpleGestureFilter detect;
String delim = new String("!!");
ImageView sstmouse;

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_control);

    detect = new SimpleGestureFilter(this, this);

    sstmouse = (ImageView)findViewById(R.id.stt_mouse);

    Display display = getWindowManager().getDefaultDisplay();
    int width = display.getWidth();

    Button left = (Button) findViewById(R.id.LeftClickButton);
    Button right = (Button) findViewById(R.id.RightClickButton);

    left.setWidth(width / 2);
    right.setWidth(width / 2);

    View touchView = (View) findViewById(R.id.TouchPad);
    touchView.setOnTouchListener(this);
}

public void onStart() {
    super.onStart();

    AppDelegate appDel = ((AppDelegate) getApplicationContext());
    sendToAppDel(new String("Mouse Sensitivity!!"+ appDel.mouse_sensitivity));

    new Thread(new Runnable() {
        AppDelegate appDel = ((AppDelegate) getApplicationContext());

        public void run() {
            while (appDel.connected) {
                try {
                    Thread.sleep(1000);
                } catch (Exception e) {
                }
                ;
                if (!appDel.connected) {
                    finish();
                }
            }
        }
    }).start();
}

// detect touch events
// and pass them to mousePadHandler method
@Override
public boolean onTouch(View v, MotionEvent event) {
    if(mousemode == false){
        mousePadHandler(event);
    }
    return true;
}
// detect keyboard event
// and send to delegate
@Override
public boolean onKey(View v, int c, KeyEvent event) {
    Log.d("ello", "" + event.getKeyCode());
    AppDelegate appDel = ((AppDelegate) getApplicationContext());

    appDel.sendMessage("S_KEY" + delim + event.getKeyCode());
    return false;
}

// send message to AppDelegate class
// to be sent to server on client desktop
private void sendToAppDel(String message) {
    AppDelegate appDel = ((AppDelegate) getApplicationContext());
    if (appDel.connected) {
        appDel.sendMessage(message);
    } else {
        finish();
    }
}

// send a mouse message
private void mousePadHandler(MotionEvent event) {
    StringBuilder sb = new StringBuilder();

    int action = event.getAction();
    int touchCount = event.getPointerCount();

    // if a single touch
    // send movement based on action
    if (touchCount == 1) {
        switch (action) {
        case 0:
            sb.append("DOWN" + delim);
            sb.append((int) event.getX() + delim);
            sb.append((int) event.getY() + delim);
            break;

        case 1:
            sb.append("UP" + delim);
            sb.append(event.getDownTime() + delim);
            sb.append(event.getEventTime());
            break;

        case 2:
            sb.append("MOVE" + delim);
            sb.append((int) event.getX() + delim);
            sb.append((int) event.getY());
            break;

        default:
            break;
        }
    }

    // if two touches
    // send scroll message
    // based off MAC osx multi touch
    // scrolls up and down
    else if (touchCount == 2) {
        sb.append("SCROLL" + delim);
        if (action == 2) {
            sb.append("MOVE" + delim);
            sb.append((int) event.getX() + delim);
            sb.append((int) event.getY());
        } else
            sb.append("DOWN");
    }

    sendToAppDel(sb.toString());
}

public void LeftButtonClickHandler(View v) {
    Log.d("eloo", "CLICKED");
    sendToAppDel("CLICK" + delim + "LEFT");
}

public void RightButtonClickHandler(View v) {
    sendToAppDel("CLICK" + delim + "RIGHT");
}

@Override
public boolean dispatchTouchEvent(MotionEvent me) {
    // Call onTouchEvent of SimpleGestureFilter class
    this.detect.onTouchEvent(me);
    return super.dispatchTouchEvent(me);
}

@Override
public void onSwipe(int direction) {

    String str = "";

    switch (direction) {
    case SimpleGestureFilter.SWIPE_RIGHT:
        if (mousemode == true) {
            str = "Swipe Right";
            sendToAppDel("CLICK" + delim + "RIGHT");
            //vibra.vibrate(1000);
        }
        break;

    case SimpleGestureFilter.SWIPE_LEFT:
        if (mousemode == true) {
            str = "Swipe Left";
            sendToAppDel("CLICK" + delim + "LEFT");
            //vibra.vibrate(1000);
        }
        break;
    }
    Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
}

@Override
public void onDoubleTap() {
    // TODO Auto-generated method stub
    Toast.makeText(this, "double tap 111 ", Toast.LENGTH_SHORT).show();
    sendToAppDel("MOUSE" + delim + "LEFT");
    //vibra.vibrate(1000);
}

@Override
public void onLongPress() {
    // TODO Auto-generated method stub
    if(mousemode == false){
        mousemode = true;
        //Toast.makeText(this, "control mode", Toast.LENGTH_SHORT).show();
        sstmouse.setImageResource(R.drawable.control_icon);
        //vibra.vibrate(1000);
    }else{
        mousemode = false;
        //Toast.makeText(this, "mouse mode", Toast.LENGTH_SHORT).show();
        sstmouse.setImageResource(R.drawable.hand_icon);
        //vibra.vibrate(1000);
    }   
}

public void clickBack(View view){

}

public void clickEsc(View view){
    sendToAppDel("CLICK" + delim + "ESC");
}

public void clickF5(View view){
    sendToAppDel("CLICK" + delim + "F5");
}

public void clickNote(View view){

}

public void clickMenu(View view){

}
}

我认为错误是在班级控制器的某个地方,但我可以找到它, 请帮我。 感谢

1 个答案:

答案 0 :(得分:1)

堆栈跟踪会有所帮助,但是该异常的原因是您的代码在Activity的NullPointerException方法中创建onCreate(),导致该方法过早退出,而不会创建Activity因此RuntimeException: Unable to start activity

解决方案:调试您的onCreate()代码,以查看您对意外null执行操作的位置,并更优雅地处理它。

可能的罪魁祸首:

left.setWidth(width / 2); // left = null

right.setWidth(width / 2); // right = null

touchView.setOnTouchListener(this); // touchView = null

对不起,没有更精确的没有堆栈跟踪。