当用户在测验中对我的问题给出正确答案时,创建带有下一个按钮的弹出窗口

问题描述 投票:1回答:1

我正在创建一个测验应用程序,我想在其中创建一个弹出窗口,当用户给出正确答案时,将出现一个弹出窗口,显示他给出了正确答案的文本和一个按钮。该按钮应将用户移至应用程序中的下一个问题。

我试图创建一个popup.xml,当用户给出正确答案时它将响应。

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="300dp"
    android:layout_height="500dp"
    android:orientation="vertical"
    android:gravity="center"
    android:layout_gravity="center"
    android:background="#242424">

    <TextView
        android:id="@+id/correct"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_margin="30dp"
        android:textSize="22sp"
        android:textColor="#FFFFFF"
        android:text="Correct! Go to Next."/>

    <Button
        android:id="@+id/button3"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>

并且称为此方法

public void onButtonShowPopupWindowClick(View view) {

        // inflate the layout of the popup window
        LayoutInflater inflater = (LayoutInflater)
                getSystemService(LAYOUT_INFLATER_SERVICE);
        View popupView = inflater.inflate(R.layout.popup_window, null);


        // create the popup window
        int width = LinearLayout.LayoutParams.WRAP_CONTENT;
        int height = LinearLayout.LayoutParams.WRAP_CONTENT;
        boolean focusable = true; // lets taps outside the popup also dismiss it
        final PopupWindow popupWindow = new PopupWindow(popupView, width, height, focusable);



        // show the popup window
        // which view you pass in doesn't matter, it is only used for the window tolken
        popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);



        // dismiss the popup window when touched
        popupView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                popupWindow.dismiss();
                return true;
            }
        });
    }

我从How to create a popup window (PopupWindow) in Android的一些答案中获得了帮助,但并没有正确执行。帮帮我。

这里是在onButtonShowPopupWindowClick()方法中添加clicklistener之后的代码

public void onButtonShowPopupWindowClick(View view) {

        // inflate the layout of the popup window
        LayoutInflater inflater = (LayoutInflater)
                getSystemService(LAYOUT_INFLATER_SERVICE);
        View popupView = inflater.inflate(R.layout.popup_window, null);
        Button btn = findViewById(R.id.button3);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i = new Intent(level_one.this,level_two.class);
                startActivity(i);
            }
        });


        // create the popup window
        int width = LinearLayout.LayoutParams.WRAP_CONTENT;
        int height = LinearLayout.LayoutParams.WRAP_CONTENT;
        boolean focusable = true; // lets taps outside the popup also dismiss it
        final PopupWindow popupWindow = new PopupWindow(popupView, width, height, focusable);



        // show the popup window
        // which view you pass in doesn't matter, it is only used for the window tolken
        popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);



        // dismiss the popup window when touched
        popupView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                popupWindow.dismiss();
                return true;
            }
        });
    }

以下跟踪显示我的应用程序崩溃

D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.geeksmind, PID: 18894
    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
        at com.example.geeksmind.level_one.onButtonShowPopupWindowClick(level_one.java:180)
        at com.example.geeksmind.level_one$1.onClick(level_one.java:96)
        at android.view.View.performClick(View.java:5619)
        at android.view.View$PerformClick.run(View.java:22298)
        at android.os.Handler.handleCallback(Handler.java:754)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:165)
        at android.app.ActivityThread.main(ActivityThread.java:6375)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:802)
android android-layout popupwindow android-popupwindow
1个回答
0
投票

问题在以下行中

Button btn = findViewById(R.id.button3);

当您膨胀一个弹出窗口时,里面有R.id.button3。因此,R.id.button3的父级是popupwindow。视图初始化应如下所示

View popupView = inflater.inflate(R.layout.popup_window, null);
Button btn = popupView.findViewById(R.id.button3);
© www.soinside.com 2019 - 2024. All rights reserved.