Skip to content Skip to sidebar Skip to footer

Android Activity Error

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

Solution 1:

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

Solution 2:

Just to help you debug your Android applications in the future the interesting lines of error are these:

10-2919:19:25.397: E/AndroidRuntime(859): Caused by: java.lang.NullPointerException
10-2919: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 :-)

Solution 3:

Buttonb= (Button) findViewById(R.id.button1);
b.setOnClickListener(newView.OnClickListener() {

@OverridepublicvoidonClick(View v) {

    Toast.makeText(MainActivity.this,"You clicked it!",Toast.LENGTH_LONG).show();
    startActivity(newIntent(MainActivity.this,Second.class));


    }

} );

Hope that beginners strucking at this kind of problem will feel great.

Solution 4:

You should make entry of second.java file into the android Manifest file. like this

<?xml version="1.0" encoding="utf-8"?><manifestxmlns:android="http://schemas.android.com/apk/res/android"package="com.example.sevenapp"android:versionCode="1"android:versionName="1.0" ><uses-sdkandroid:minSdkVersion="8"android:targetSdkVersion="17" /><applicationandroid:allowBackup="true"android:icon="@drawable/ic_launcher"android:label="@string/app_name"android:theme="@style/AppTheme" ><activityandroid:name="com.example.sevenapp.MainActivity"android:label="@string/app_name" ><intent-filter><actionandroid:name="android.intent.action.MAIN" /><categoryandroid:name="android.intent.category.LAUNCHER" /></intent-filter></activity><activityandroid:name="second"></activity>///Make entry here
    </application></manifest>

Post a Comment for "Android Activity Error"