如何使用android studio中的弹出窗口按钮打开新活动?

时间:2017-03-06 08:16:31

标签: java android xml android-layout popupmenu

我有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 Code

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

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) 

4 个答案:

答案 0 :(得分:2)

May get the question wrong but was able to call GameController activity from button in you popmenu xml:

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

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

in main activity

答案 1 :(得分:1)

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

答案 2 :(得分:1)

您必须在意图

后关闭弹出窗口
          V1      V2     V3     V4
 rs200140498    chr1    861315  GG
 rs371217242    chr1    861329  --
 rs200686669    chr1    861349  CC
 rs370046315    chr1    861357  CC
 rs374110379    chr1    861521  --
 rs74045401     chr1    861530  GG
 rs377418023    chr1    865394  CC
 rs79027658     chr1    865438  --
 rs202189913    chr1    865488  AA
 rs370992396    chr1    865543  GG

答案 3 :(得分:0)

openNewActivity()是emty。改变这个

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