package com.ebonybutler.cexample3;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Main extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(Main.this, Second.class));
}
});
}
}
Every time I try to run it, the error
‘Sorry! the application example3 (processcom.ebonybutler.cexample3)
has stopped unexpectedly. Please try again.’
I can’t figure out what keeps crashing it at launch because all of the java files seem fine or at least aren’t any bugs. Please help! I’ve added the information from my logcat below but I’m having trying interpreting what its saying to me.
11-02 09:29:10.261: E/AndroidRuntime(276): FATAL EXCEPTION: main
11-02 09:29:10.261: E/AndroidRuntime(276): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ebonybutler.cexample3/com.ebonybutler.cexample3.Main}: java.lang.NullPointerException
11-02 09:29:10.261: E/AndroidRuntime(276): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
11-02 09:29:10.261: E/AndroidRuntime(276): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-02 09:29:10.261: E/AndroidRuntime(276): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-02 09:29:10.261: E/AndroidRuntime(276): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-02 09:29:10.261: E/AndroidRuntime(276): at android.os.Handler.dispatchMessage(Handler.java:99)
11-02 09:29:10.261: E/AndroidRuntime(276): at android.os.Looper.loop(Looper.java:123)
11-02 09:29:10.261: E/AndroidRuntime(276): at android.app.ActivityThread.main(ActivityThread.java:4627)
11-02 09:29:10.261: E/AndroidRuntime(276): at java.lang.reflect.Method.invokeNative(Native Method)
11-02 09:29:10.261: E/AndroidRuntime(276): at java.lang.reflect.Method.invoke(Method.java:521)
11-02 09:29:10.261: E/AndroidRuntime(276): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-02 09:29:10.261: E/AndroidRuntime(276): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-02 09:29:10.261: E/AndroidRuntime(276): at dalvik.system.NativeStart.main(Native Method)
11-02 09:29:10.261: E/AndroidRuntime(276): Caused by: java.lang.NullPointerException
11-02 09:29:10.261: E/AndroidRuntime(276): at com.ebonybutler.cexample3.Main.onCreate(Main.java:21)
11-02 09:29:10.261: E/AndroidRuntime(276): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-02 09:29:10.261: E/AndroidRuntime(276): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
11-02 09:29:10.261: E/AndroidRuntime(276): ... 11 more
By my count this is occurring when you set the listener for your button. Are you sure that this button still has the ID button1 in your layout’s xml file? This error would occur if that ID now longer existed in your layout
Edit
so in code terms, this is what I am saying. I am saying that maybe you used to have a Button in your main.xml file with the id “button1”. (see below)
but perhaps now, you may have changed the id in the layout xml and not changed your code (see ‘newid’ below)
In this case, findViewById would return null, since no such id would exist in your main.xml file anymore.