如何在 android 工作室中使用弹出 window 按钮打开新的 activity?

How to open new activity using popup window button in android studio?

我有 MainActivity.class 我已经创建了弹出菜单 & 有几个按钮我需要的是我需要将用户发送到新的 activity 我有 created.my 代码正在工作在 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>

AndroidManifest 代码

<?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.access0(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.access0(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) 

打开新 Activity() 为空。改变这个

<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/Novice"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:text="Novice" />
final Button btnNewPopup = (Button) findViewById(R.id.newgame);
btnNewPopup.setOnClickListener(new OnClickListener() {
    @Override public void onClick (View arg0){
        ...
        View popupView = layoutInflater.inflate(R.layout.popup_newgame, null);
        ...

        final Button btnNewNovise = (Button) popupView.findViewById(R.id.Novice);
        btnNewNovise.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Intent myIntent = new Intent(MainActivity.this, GameController.class);
                startActivity(myIntent);
            }
        });
    }
});

你必须在 intent 之后关闭你的弹出窗口

Intent myIntent = new Intent(MainActivity.this,GameController.class);
startActivity(myIntent);
yourpopup.dismiss();

可能会答错问题,但可以通过弹出菜单中的按钮调用 GameController activity xml:

 <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