I am newbie in android. In android I trying to run a simple work. From my main.xml(http://pastebin.com/Mhgmpj4w) I have one button( my main.java(http://pastebin.com/b5irLVTc) ) it will switch to another activity the second.xml(http://pastebin.com/F92VxSEZ) where it will show some text ( my second.java(http://pastebin.com/MR3L5WXL) ) but there are some errors(http://pastebin.com/KH2wDh7b). I am a newbie please help me.
main.xml:
<?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" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
main.java:
package com.rana.activities;
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;
public class Main extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b = (Button) findViewById(android.R.id.button1);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(Main.this, Second.class));
}
});
}
}
second.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is the second activity" />
</LinearLayout>
second.java:
package com.rana.activities;
import android.app.Activity;
import android.os.Bundle;
public class Second extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
}
}
logcat:
10-29 19:19:25.377: D/AndroidRuntime(859): Shutting down VM
10-29 19:19:25.377: W/dalvikvm(859): threadid=1: thread exiting with uncaught exception (group=0x40015560)
10-29 19:19:25.397: E/AndroidRuntime(859): FATAL EXCEPTION: main
10-29 19:19:25.397: E/AndroidRuntime(859): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.rana.activities/com.rana.activities.Main}: java.lang.NullPointerException
10-29 19:19:25.397: E/AndroidRuntime(859): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
10-29 19:19:25.397: E/AndroidRuntime(859): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
10-29 19:19:25.397: E/AndroidRuntime(859): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-29 19:19:25.397: E/AndroidRuntime(859): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
10-29 19:19:25.397: E/AndroidRuntime(859): at android.os.Handler.dispatchMessage(Handler.java:99)
10-29 19:19:25.397: E/AndroidRuntime(859): at android.os.Looper.loop(Looper.java:123)
10-29 19:19:25.397: E/AndroidRuntime(859): at android.app.ActivityThread.main(ActivityThread.java:3683)
10-29 19:19:25.397: E/AndroidRuntime(859): at java.lang.reflect.Method.invokeNative(Native Method)
10-29 19:19:25.397: E/AndroidRuntime(859): at java.lang.reflect.Method.invoke(Method.java:507)
10-29 19:19:25.397: E/AndroidRuntime(859): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-29 19:19:25.397: E/AndroidRuntime(859): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-29 19:19:25.397: E/AndroidRuntime(859): at dalvik.system.NativeStart.main(Native Method)
10-29 19:19:25.397: E/AndroidRuntime(859): Caused by: java.lang.NullPointerException
10-29 19:19:25.397: E/AndroidRuntime(859): at com.rana.activities.Main.onCreate(Main.java:18)
10-29 19:19:25.397: E/AndroidRuntime(859): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-29 19:19:25.397: E/AndroidRuntime(859): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
10-29 19:19:25.397: E/AndroidRuntime(859): ... 11 more
<-------LogCat------------------->
Replace
android.R.id.button1this withR.id.button1and import your own packageRclass inmain.java. It will solve the problem. In this way it will allocated the memory from your application and hence it will not search for the global resouces