Android image button open new activity

可紊 提交于 2020-01-07 08:26:08

问题


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>

回答1:


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? 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.




回答2:


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>



回答3:


 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);

            }
        });


来源:https://stackoverflow.com/questions/24521485/android-image-button-open-new-activity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!