Arun M R
Arun M R

Reputation: 41

Intent not working (Showing errors)

while starting intent on image button click "Intent intent = new Intent(this,Settings.class);" showing error in eclipse. Any thing wrong on my code? Here's my code.. Help appreciated..

imageButton =(ImageButton)findViewById(R.id.imageButton4);
        imageButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                Intent intent = new Intent(this,Settings.class);
                startActivity(intent);

            }
        });

Upvotes: 0

Views: 269

Answers (6)

Mohd Saleem
Mohd Saleem

Reputation: 94

Intent intent = new Intent(this,Settings.class);

use this line of code

    Intent intent = new Intent(yourclassName.this,Settings.class);

    Because you using anonymus class for OnClickListener.
    So you can provide the reference with class name. this

Upvotes: 0

SAURABH_12
SAURABH_12

Reputation: 2310

You have to do two things First start your activity like this

Intent intent = new Intent(getBaseContext, Setting.class); startActivity(intent);

Second thing Mention this class into Android manifest.xml inside application tag below the by default activitytag

I think this way you can resolve your problem.

Upvotes: 1

philip
philip

Reputation: 1302

do this

imageButton =(ImageButton)findViewById(R.id.imageButton4);
        imageButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                Intent intent = new Intent(YourCurrentCLass.this,Settings.class);
                startActivity(intent);

            }
        });

and check your manifest

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.sampledatabase"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

if you only see <category android:name="android.intent.category.LAUNCHER" />

and no <category android:name="android.intent.category.DEFAULT" />

create a new activity like this

<activity
            android:name=".Settings"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="name.of.your.package.Settings" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

remember to put it inside your <application tag same as your .MainActivity

Upvotes: 0

Venkatesh S
Venkatesh S

Reputation: 5492

You should use getApplicationContext() instead of this

public void onClick(View v) {
       Intent intent = new Intent(getApplicationContext(), Settings.class);
       startActivity(intent);
      }
});

Upvotes: 1

Bhavesh Patadiya
Bhavesh Patadiya

Reputation: 25830

try below code.

 public void onClick(View v) {


                Intent intent = new Intent(yourclassName.this,Settings.class);
                startActivity(intent);

            }
        });

Upvotes: 0

Ramindu Weeraman
Ramindu Weeraman

Reputation: 354

Add your Settings activity in AndroidManifest.xml

Upvotes: -1

Related Questions