Reputation: 5186
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------------------->
Upvotes: 0
Views: 2819
Reputation: 7553
You should make entry of second.java file into the android Manifest file. like this
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sevenapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.sevenapp.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="second"></activity>///Make entry here
</application>
</manifest>
Upvotes: 0
Reputation: 51
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"You clicked it!",Toast.LENGTH_LONG).show();
startActivity(new Intent(MainActivity.this,Second.class));
}
} );
Hope that beginners strucking at this kind of problem will feel great.
Upvotes: 0
Reputation: 9520
Replace android.R.id.button1
this with R.id.button1
and import your own package R
class in main.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
Upvotes: 8
Reputation: 1752
Just to help you debug your Android applications in the future the interesting lines of error are these:
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)
In line 18 of your Main.java file we have b.setOnClickListener(new OnClickListener() and a NullPointerException. This exception is caused by line 17 in your code with Button b = (Button) findViewById(android.R.id.button1);
As Drax mentioned the error can easily be solved by supplying the correct id.
Good luck with your future developing in Android I hope you'll find it fun :-)
Upvotes: 1