I just implemented the onClickListener. The App should switch between two layouts. On each layout there is one button. After touching the button the Layout should change.
I searched trough various tutorials and documentary, my code should be correct, I simply don’t see the error. Eclipse also says the code is ok.
package ralationship.v1;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class RelationshipActivity extends Activity implements OnClickListener{
/** Called when the activity is first created. */
private Button OptionsButton;
private Button SaveButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SaveButton = (Button)findViewById(R.id.btnSave);
OptionsButton = (Button)findViewById(R.id.btnOptions);
OptionsButton.setOnClickListener(this);
SaveButton.setOnClickListener(this);
}
public void onClick(View v) {
if(v.getId() == (R.id.btnOptions)){
setContentView(R.layout.options);
}else if (v.getId() == (R.id.btnSave)){
setContentView(R.layout.main);
}
}
}
Does anyone find an error?
//EDIT:
Here is the LogCat Report:
01-26 15:47:29.273: D/dalvikvm(336): GC_EXTERNAL_ALLOC freed 49K, 53% free 2549K/5379K, external 1625K/2137K, paused 67ms
01-26 15:47:29.453: D/AndroidRuntime(336): Shutting down VM
01-26 15:47:29.462: W/dalvikvm(336): threadid=1: thread exiting with uncaught exception (group=0x40015560)
01-26 15:47:29.473: E/AndroidRuntime(336): FATAL EXCEPTION: main
01-26 15:47:29.473: E/AndroidRuntime(336): java.lang.RuntimeException: Unable to start activity ComponentInfo{ralationship.v1/ralationship.v1.RelationshipActivity}: java.lang.NullPointerException
01-26 15:47:29.473: E/AndroidRuntime(336): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
01-26 15:47:29.473: E/AndroidRuntime(336): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
01-26 15:47:29.473: E/AndroidRuntime(336): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-26 15:47:29.473: E/AndroidRuntime(336): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
01-26 15:47:29.473: E/AndroidRuntime(336): at android.os.Handler.dispatchMessage(Handler.java:99)
01-26 15:47:29.473: E/AndroidRuntime(336): at android.os.Looper.loop(Looper.java:123)
01-26 15:47:29.473: E/AndroidRuntime(336): at android.app.ActivityThread.main(ActivityThread.java:3683)
01-26 15:47:29.473: E/AndroidRuntime(336): at java.lang.reflect.Method.invokeNative(Native Method)
01-26 15:47:29.473: E/AndroidRuntime(336): at java.lang.reflect.Method.invoke(Method.java:507)
01-26 15:47:29.473: E/AndroidRuntime(336): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-26 15:47:29.473: E/AndroidRuntime(336): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-26 15:47:29.473: E/AndroidRuntime(336): at dalvik.system.NativeStart.main(Native Method)
01-26 15:47:29.473: E/AndroidRuntime(336): Caused by: java.lang.NullPointerException
01-26 15:47:29.473: E/AndroidRuntime(336): at ralationship.v1.RelationshipActivity.onCreate(RelationshipActivity.java:22)
01-26 15:47:29.473: E/AndroidRuntime(336): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-26 15:47:29.473: E/AndroidRuntime(336): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
01-26 15:47:29.473: E/AndroidRuntime(336): ... 11 more
01-26 15:47:33.633: I/Process(336): Sending signal. PID: 336 SIG: 9
I assume that your
saveButtonis in theR.layout.optionslayout? If so, it is not available to be found when your activity is first created, as you only have the content fromR.layout.main. You should instead put the linesinside the
onClickListener, after you have changed the layout withsetContentLayout()to the correct layout.