Siddhant
Siddhant

Reputation: 53

OnClickListener crashes app

I'm learning android development. I created a simple layout consisting of a button (id:button1). I added an OnClickListener to this button, which when clicked, shows the next page (layout). There appears to be no errors in the code, but when I run it, it simply crashes. I tried it using android 2.3.3 and 2.2 emulators, but no success. when I comment out the onclicklistener part, the app runs. i searched through various sites and questions, but no success. Here is the java code:

package com.sid.next;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class mySplash extends Activity {
    /** Called when the activity is first created. */

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final Button b1 = (Button)findViewById(R.id.button1);
        b1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                startActivity(new Intent("com.sid.next.SHOWMENU"));
            }
        });

    }
}

Edit1: stacktrace

Edit2: main.xml

Edit3: [solved!] i didn't have any contentview set for the myMenu.java activity. Thanks anyways!

edit4: changed android.R.id.button1 to R.id.button1

Upvotes: 0

Views: 737

Answers (5)

Siddhant
Siddhant

Reputation: 53

okay. so finally i figured it out myself! i hadn't set any contentView for the myMenu.java class.

also changed android.R.id.button1 to R.id.button1. (credit: Imran Rana)

thank you everyone!

Upvotes: 1

Imran Rana
Imran Rana

Reputation: 11909

Replace

 final Button b1 = (Button)findViewById(android.R.id.button1);

By:

 final Button b1 = (Button)findViewById(R.id.button1);

That is replace android.R by R.

Upvotes: 1

sachi
sachi

Reputation: 2132

Try this

Intent i = new Intent(new Intent(CURRENT_SCREEN_NAME.this, NEXT_SCREEN_NAME.class));
startActivity(i);

and don't forget to write this in manifest below the activity.

<activity android:name=".NEXT_SCREEN_NAME" ></activity>

Upvotes: 0

Avi Kumar
Avi Kumar

Reputation: 4433

Rathter calling activity this way

startActivity(new Intent("com.sid.next.SHOWMENU"));

try this way

Intent g = new Intent(new Intent(mySplash .this, SHOWMENU.class));
            startActivity(g);

Upvotes: 0

Bhavin
Bhavin

Reputation: 6010

If SHOWMENU is an Activity then do Declare in Manifest File.

By the given data i think this is your option remaining.

<activity
        android:name=".SHOWMENU"
        android:label="@string/app_name" />

Add inside the <application > </application> Tags in your AndroidManifest.xml file

Just copy and paste in your xml

<application
    android:icon="@drawable/icon"
    android:label="@string/app_name" >
    <activity
        android:name="com.sid.next.SHOWMENU"
        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="com.sid.next.SHOWMENU"
        android:label="@string/app_name" />
</application>

Upvotes: 0

Related Questions