java - 如何在 android studio 中使用弹出窗口按钮打开新 Activity ?

标签 java android xml android-layout popupmenu

我有 MainActivity.class n 我已经创建了弹出菜单并且有几个按钮我需要的是我需要将用户发送到我创建的新 Activity 。我的代码正在运行在 MainActivity.xml 上创建的按钮但是当我给弹出菜单按钮 ID 未启动我的应用程序。 期待支持。

package com.example.sampathmunaweera.mobilecw;


import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.view.View.OnClickListener;
import android.view.View;

import android.app.Activity;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.view.ViewGroup.LayoutParams;
import android.widget.PopupMenu;




public class MainActivity extends AppCompatActivity {


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



        Button btnexit = (Button) findViewById(R.id.exit);
        btnexit.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // exit from app
            finish();
            System.exit(0);
        }



    });

        // end exit function



        final Button btnOpenPopup = (Button)findViewById(R.id.about);
        btnOpenPopup.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View arg0) {
                LayoutInflater layoutInflater
                        = (LayoutInflater)getBaseContext()
                        .getSystemService(LAYOUT_INFLATER_SERVICE);
                View popupView = layoutInflater.inflate(R.layout.popup_about, null);
                final PopupWindow popupWindow = new PopupWindow(
                        popupView,
                        LayoutParams.WRAP_CONTENT,
                        LayoutParams.WRAP_CONTENT);

                Button btnDismiss = (Button)popupView.findViewById(R.id.dismiss);
                btnDismiss.setOnClickListener(new OnClickListener(){

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        popupWindow.dismiss();
                    }});

                //popupWindow.showAsDropDown(btnOpenPopup, 50, 50);
                popupWindow.showAtLocation(btnOpenPopup, Gravity.CENTER, 0, 0);


            }});



        //pop up new game

        final Button btnNewPopup = (Button)findViewById(R.id.newgame);
        btnNewPopup.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View arg0) {
                LayoutInflater layoutInflater
                        = (LayoutInflater)getBaseContext()
                        .getSystemService(LAYOUT_INFLATER_SERVICE);
                View popupView = layoutInflater.inflate(R.layout.popup_newgame, null);
                final PopupWindow popupWindow = new PopupWindow(
                        popupView,
                        LayoutParams.WRAP_CONTENT,
                        LayoutParams.WRAP_CONTENT);

                Button btnDismiss = (Button)popupView.findViewById(R.id.dismiss);
                btnDismiss.setOnClickListener(new OnClickListener(){

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        popupWindow.dismiss();
                    }});

                //popupWindow.showAsDropDown(btnOpenPopup, 50, 50);
                popupWindow.showAtLocation(btnOpenPopup, Gravity.CENTER, 0, 0);


            }});


         //open new activity

        final Button btnNewNovise = (Button)findViewById(R.id.Novice);
        btnNewNovise.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View arg0) {
                Intent myIntent = new Intent(MainActivity.this,
                        GameController.class);
                startActivity(myIntent);



            }});




    }
    public void openNewActivity (View view) {

    }

    }

弹出菜单xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="@android:color/background_light">
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_margin="1dp"
        android:background="@android:color/darker_gray">
        >
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_margin="20dp">

            <TextView
                android:id="@+id/texttitle1"
                android:textStyle="bold"
                android:textSize="25dp"
                android:textColor="#891800"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Select Game Level" />

            <Button
                android:id="@+id/Novice"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:onClick="openNewActivity"
                android:text="Novice" />

            <Button
                android:id="@+id/Easy"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Easy" />

            <Button
                android:id="@+id/Hard"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Hard" />

            <Button
                android:id="@+id/Guru"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Guru" />

            <Button
                android:id="@+id/dismiss"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Back to Menu" />




        </LinearLayout>
    </LinearLayout>
</LinearLayout>

Android list 代码

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.sampathmunaweera.mobilecw">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:installLocation="preferExternal"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:label="@string/app_name"
            android:name=".GameController"
            android:windowSoftInputMode="stateHidden"></activity>
    </application>

</manifest>

logcat 错误

03-06 14:19:20.982 4792-4792/com.example.sampathmunaweera.mobilecw E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                     Process: com.example.sampathmunaweera.mobilecw, PID: 4792
                                                                                     java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sampathmunaweera.mobilecw/com.example.sampathmunaweera.mobilecw.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                                                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
                                                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
                                                                                         at android.app.ActivityThread.access$800(ActivityThread.java:151)
                                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
                                                                                         at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                         at android.os.Looper.loop(Looper.java:135)
                                                                                         at android.app.ActivityThread.main(ActivityThread.java:5254)
                                                                                         at java.lang.reflect.Method.invoke(Native Method)
                                                                                         at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
                                                                                      Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                                                                                         at com.example.sampathmunaweera.mobilecw.MainActivity.onCreate(MainActivity.java:117)
                                                                                         at android.app.Activity.performCreate(Activity.java:5990)
                                                                                         at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
                                                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
                                                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
                                                                                         at android.app.ActivityThread.access$800(ActivityThread.java:151) 
                                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
                                                                                         at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                         at android.os.Looper.loop(Looper.java:135) 
                                                                                         at android.app.ActivityThread.main(ActivityThread.java:5254) 
                                                                                         at java.lang.reflect.Method.invoke(Native Method) 
                                                                                         at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
                                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 

最佳答案

可能会出错,但能够从弹出菜单 xml 中的按钮调用 GameController Activity :

 <Button
                android:id="@+id/Novice"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:onClick="openNewActivity"
                android:text="Novice" />`

我刚刚添加了这些行:

public void openNewActivity (View view) {
        if(view.getId()==R.id.Novice){
            startActivity(new Intent(this,GameController.class));
        }
} 

在主要 Activity 中

关于java - 如何在 android studio 中使用弹出窗口按钮打开新 Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42620613/

相关文章:

java - 用正则表达式中的另一组字符(成对)替换一组字符 : "&", "&amp;" "<"、 "<"等

java - 为什么别名在 java 方法中不起作用?

java - 如何在java中找到两个整数数组之间的相关性

java - Spring XmlBeanFactory 已弃用

c# - RegionInfo.CurrentRegion 基于设备语言 - c#

android - ajaxSuccess之后无法获取getCurrentUser()

java - LibGDX 类似乎没有调用父类(super class)构造函数,这怎么可能?

android - iOS Android 和 windows 推送通知

xml - 在这种情况下如何添加唯一约束?

python - 如何搜索文件特定的 XML 代码模式