简体   繁体   English

Android图像按钮打开新活动

[英]Android image button open new activity

I would like to open new activity from an image button in android. 我想从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 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}; 错误:android.content.ActivityNotFoundException:无法找到显式活动类{dev.com.test / dev.com.test.menu}; have you declared this activity in your AndroidManifest.xml? 您是否在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 只要确保您的AndroidManifest看起来像这样

    <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? 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. 每当您创建新活动时,请确保该活动已在application标签内的AndroidManifest.xml注册。

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

            }
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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