When I try to run it in my AVD, it gives me an error that says:
Sorry! the application Hello World (process com.duncan.hello.world) has stopped unexpectedly. Please try again.
I think it has something to do with adding in the code for the second button, because it worked fine before that. This is the code that i have in my main java file:
package com.duncan.hello.world;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import com.duncan.hello.world.R;
public class HelloWorldActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button aButton;
aButton = (Button) this.findViewById(R.id.button1);
aButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent (HelloWorldActivity.this, OtherActivity.class);
startActivity(i);
}
});
Button newButton;
newButton = (Button) this.findViewById(R.id.meh);
newButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(HelloWorldActivity.this, Meh.class);
startActivity(i);
}
});
}
}
this is the other.xml(the layout where meh is):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="you...are...on...page...2...!!!" />
<Button
android:id="@+id/meh"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="meh"/>
<Button
android:id="@+id/p40"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="this go to p40" />
</LinearLayout>
here is the Meh.java:
package com.duncan.hello.world;
import com.duncan.hello.world.R;
import android.app.Activity;
import android.os.Bundle;
public class Meh extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.meh);
}
}
logcat errors:
12-07 14:40:00.840: E/AndroidRuntime(497): FATAL EXCEPTION: main
12-07 14:40:00.840: E/AndroidRuntime(497): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.duncan.hello.world/com.duncan.hello.world.HelloWorldActivity}: java.lang.NullPointerException
12-07 14:40:00.840: E/AndroidRuntime(497): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
12-07 14:40:00.840: E/AndroidRuntime(497): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
12-07 14:40:00.840: E/AndroidRuntime(497): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
12-07 14:40:00.840: E/AndroidRuntime(497): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
12-07 14:40:00.840: E/AndroidRuntime(497): at android.os.Handler.dispatchMessage(Handler.java:99)
12-07 14:40:00.840: E/AndroidRuntime(497): at android.os.Looper.loop(Looper.java:123)
12-07 14:40:00.840: E/AndroidRuntime(497): at android.app.ActivityThread.main
(ActivityThread.java:4627)
12-07 14:40:00.840: E/AndroidRuntime(497): at java.lang.reflect.Method.invokeNative(Native Method)
12-07 14:40:00.840: E/AndroidRuntime(497): at java.lang.reflect.Method.invoke(Method.java:521)
12-07 14:40:00.840: E/AndroidRuntime(497): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-07 14:40:00.840: E/AndroidRuntime(497): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-07 14:40:00.840: E/AndroidRuntime(497): at dalvik.system.NativeStart.main(Native Method)
12-07 14:40:00.840: E/AndroidRuntime(497): Caused by: java.lang.NullPointerException
12-07 14:40:00.840: E/AndroidRuntime(497): at com.duncan.hello.world.HelloWorldActivity.onCreate(HelloWorldActivity.java:32)
12-07 14:40:00.840: E/AndroidRuntime(497): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-07 14:40:00.840: E/AndroidRuntime(497): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
12-07 14:40:00.840: E/AndroidRuntime(497): … 11 more
Can you give your logcat output? If the problems occurred after you added the second button, then is it because Meh.java is not in your mainfest file?
Logcat output can be found if you switch views (top right hand corner) to DDMS, and then click LogCat. Click the W or the E in the right hand side of the logcat window, it should show you the warnings and errors that occurred. Your crash will be red, so copy paste that part to show us the problem.
Your problem is that you are declaring
before you call the super onCreate method. You have to call super.onCreate(bundleInstanceState) before you do anything else.
What you want to do is more like:
You mentioned that the ‘meh’ button is in other.xml. It is supposed to be in main.xml. Add a button with the id ‘meh’ in main.xml.