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

Help me, I take this error when try open class "Controller" in pre-class with code

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

and this is class Controller package 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){

}
}

i think mistake is somewhere in class controller but i can find it, please help me. Thank

3
  • Post your AndroidManifest.xml code. May be you forgot to include activity name in manifest. Commented Mar 31, 2014 at 8:35
  • pls add the entire log Commented Mar 31, 2014 at 8:36
  • Yeah. Pls post your AndroidManifest.xml. This error is quite usual. Commented Mar 31, 2014 at 8:39

1 Answer 1

1

A stack trace will be helpful, but the reason for that exception is that your code is creating a NullPointerException in the Activity's onCreate() method which causes the method to prematurely exit, leaving the Activity not created, hence the RuntimeException: Unable to start activity.

Solution: debug your onCreate() code to see where you're performing an operation on the unexpected null and handle it more gracefully.

Possible culprits:

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

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

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

Sorry, can't be more precise w/o a stacktrace.

Sign up to request clarification or add additional context in comments.

2 Comments

i was tried by check if left, right or touchView = null, so it not work :(
Best to proceed with actual debugging. Maybe this question+answer would be helpful in that regard: stackoverflow.com/questions/8551818/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.