【问题标题】:java.lang.RuntimeException: Unable to start activity ComponentInfo{}: java.lang.NullPointerExceptionjava.lang.RuntimeException:无法启动活动 ComponentInfo{}:java.lang.NullPointerException
【发布时间】:2014-05-10 12:22:22
【问题描述】:
java.lang.RuntimeException: 
Unable to start     activity ComponentInfo{org.example.screens/edu.dongthang.controller.Controller}: 
java.lang.NullPointerException

帮帮我,当我尝试使用代码在预课中打开类“控制器”时出现此错误

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){

}
}

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

【问题讨论】:

  • 发布您的 AndroidManifest.xml 代码。可能是您忘记在清单中包含活动名称。
  • 请添加整个日志
  • 是的。请发布您的 AndroidManifest.xml。这个错误很常见。

标签: android nullpointerexception


【解决方案1】:

堆栈跟踪会有所帮助,但该异常的原因是您的代码在 Activity 的 onCreate() 方法中创建了一个 NullPointerException,这会导致该方法过早退出,导致未创建 Activity,因此 @ 987654324@.

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

可能的罪魁祸首:

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

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

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

抱歉,没有堆栈跟踪,再精确不过了。

【讨论】:

猜你喜欢
  • 2023-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-28
  • 2014-05-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多