简体   繁体   中英

Android image button open new activity

I would like to open new activity from an image button in android. I tried with this code, but doesn't work.

main.class

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


public class main extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    openMenu();
}

public void openMenu() {

    final Context context = this;

    ImageButton imgbtn = (ImageButton) findViewById(R.id.menu_button);

    imgbtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            Intent menu = new Intent(context, menu.class);
            startActivity(menu);

        }

    });
}

}

menu.class

public class menu extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.menu);
}

the error: android.content.ActivityNotFoundException: Unable to find explicit activity class {dev.com.test/dev.com.test.menu}; have you declared this activity in your AndroidManifest.xml? My answer: yes

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

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

No, you haven't included it in your manifest. What you've included is the main activity. But you must include every activity, including this new one that you are trying to create. Here is an example (you will probably need to change the package name):

<application
android:allowBackup="true"
android:icon="@drawable/icon"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
    <activity
    android:name=".main"
    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.test.menu"
    android:label="My second activity"
    android:parentActivityName="com.test.main" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.test.main" />
    </activity>

</application>

Just make sure your AndroidManifest looks like this

    <application
         android:allowBackup="true"
         android:icon="@drawable/icon"
         android:label="@string/app_name"
         android:theme="@style/AppTheme" >
    <activity
    android:name=".main"
    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=".menu">      // you need to add these 3 lines inside application tag.
    </activity>

</application>

The error android.content.ActivityNotFoundException: Unable to find explicit activity class {dev.com.test/dev.com.test.menu}; have you declared this activity in your AndroidManifest.xml? android.content.ActivityNotFoundException: Unable to find explicit activity class {dev.com.test/dev.com.test.menu}; have you declared this activity in your AndroidManifest.xml? is very much straight to the point. Whenever you create a new activity, make sure that activity is registered in AndroidManifest.xml inside the application tag.

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

            @Override
            public void onClick(View v) {

                Intent intent = new Intent(context, MainActivity2.class);
                startActivity(intent);

            }
        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM