【问题标题】:How to open new activity using popup window button in android studio?如何在 android studio 中使用弹出窗口按钮打开新活动?
【发布时间】:2017-03-06 08:16:30
【问题描述】:

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

【问题讨论】:

  • PopupMenu 你的代码在哪里?
  • 你能写出你的清单代码吗?
  • @muhammed 更新了它。当我使用 mainactivity.xml 中的按钮时,此代码运行良好,但我需要做的是通过弹出菜单按钮打开新活动
  • @PhanVanLinh 弹出菜单代码已经存在
  • @SampathMJay PopupMenu 或 PopupWindow。我只在您的代码中看到 PopupWindow

标签: java android xml android-layout popupmenu


【解决方案1】:

可能会弄错问题,但能够从 popmenu xml 中的按钮调用 GameController 活动:

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

在主要活动中

【讨论】:

    【解决方案2】:
    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);
                }
            });
        }
    });
    

    【讨论】:

    • 这个“layoutInflater”库是否显示错误
    • 无法解析符号“layoutInflater”
    • 在我的回答中,在......我的意思是你应该保留你当前的代码
    【解决方案3】:

    你必须在意图后关闭你的弹出窗口

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

    【讨论】:

    • yourpopup.dismiss();没有显示错误说无法解决方法dismiss();
    【解决方案4】:

    openNewActivity() 是空的。改变这个

    <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" />
    

    【讨论】:

    • done 然后什么应用没有在午餐,没有错误,但不幸的是它的显示 mobileCW 已停止。
    猜你喜欢
    • 2015-10-26
    • 2011-06-15
    • 1970-01-01
    • 1970-01-01
    • 2015-04-26
    • 1970-01-01
    • 2021-03-21
    • 1970-01-01
    • 2022-05-23
    相关资源
    最近更新 更多