简体   繁体   English

通过弹出窗口进行按钮操作

[英]Button actions through a pop-up window

My question is if I am able to use the button that I generated through a pop-up window in order to reference another activity. 我的问题是,是否可以使用通过弹出窗口生成的按钮来引用其他活动。 When I tried to use an intent it gave me a null pointer error. 当我尝试使用意图时,它给了我一个空指针错误。

Here is my onCreate method for my dummy class: 这是我的虚拟类的onCreate方法:

private Button finishButton, homeButton;
    private PopupWindow mPopupWindow1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sensor);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);

        }

        mVisible = true;

        mControlsView = findViewById(R.id.fullscreen_content_controls);
        mContentView = findViewById(R.id.fullscreen_content);


        // Set up the user interaction to manually show or hide the system UI.
        mContentView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                toggle();
            }
        });

        // Upon interacting with UI controls, delay any scheduled hide()
        // operations to prevent the jarring behavior of controls going away
        // while interacting with the UI.
        findViewById(R.id.finish_button).setOnTouchListener(mDelayHideTouchListener);

        //OnCreate creates the view with this pop-up enable to the button
        View popupView1 = getLayoutInflater().inflate(R.layout.pop_up_window, null);

        mPopupWindow1 = new PopupWindow(popupView1, android.app.ActionBar.LayoutParams.WRAP_CONTENT, android.app.ActionBar.LayoutParams.WRAP_CONTENT, true);
        mPopupWindow1.setTouchable(true);
        mPopupWindow1.setOutsideTouchable(true);
        mPopupWindow1.setBackgroundDrawable(new BitmapDrawable(getResources(), (Bitmap) null));
        mPopupWindow1.setAnimationStyle(R.style.PopupAnimation);

        mPopupWindow1.getContentView().setFocusableInTouchMode(true);
        mPopupWindow1.getContentView().setFocusable(true);
        mPopupWindow1.getContentView().setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_MENU && event.getRepeatCount() == 0
                        && event.getAction() == KeyEvent.ACTION_DOWN) {
                    if (mPopupWindow1 != null && mPopupWindow1.isShowing()) {
                        mPopupWindow1.dismiss();
                    }
                    return true;
                }
                return false;
            }
        });
        finishButton = (Button) findViewById(R.id.finish_button);
        finishButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                mPopupWindow1.showAtLocation(findViewById(R.id.finish_button), Gravity.CENTER, 0, 0);

            }

        });

        homeButton = (Button) findViewById(R.id.home_button);
        if(homeButton != null){
        homeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(SensorActivity.this, MainActivity.class);
                startActivity(intent);
            }
        });}
    }

I want the button produced from the pop-up to take me to the main activity. 我希望从弹出窗口中产生的按钮将我带到主要活动。

Any pointers will help! 任何指针都会有所帮助!

Edit: The code with my intent is: 编辑:我的意图的代码是:

homeButton = (Button) findViewById(R.id.home_button);
        homeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(SensorActivity.this, MainActivity.class);
                startActivity(intent);
            }
        });}

And the corresponding logcat for the error is: 该错误的相应logcat是:

 FATAL EXCEPTION: main
                                                                                  Process: com.example.tannerhenry.insoleapp, PID: 5180
                                                                                         java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tannerhenry.insoleapp/com.example.tannerhenry.insoleapp.SensorActivity}: java.lang.NullPointerException
                                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
                                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
                                                                                             at android.app.ActivityThread.access$800(ActivityThread.java:135)
                                                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
                                                                                             at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                             at android.os.Looper.loop(Looper.java:136)
                                                                                             at android.app.ActivityThread.main(ActivityThread.java:5017)
                                                                                             at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                             at java.lang.reflect.Method.invoke(Method.java:515)
                                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
                                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
                                                                                             at dalvik.system.NativeStart.main(Native Method)
                                                                                          Caused by: java.lang.NullPointerException
                                                                                             at com.example.tannerhenry.insoleapp.SensorActivity.onCreate(SensorActivity.java:156)
                                                                                             at android.app.Activity.performCreate(Activity.java:5231)
                                                                                             at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
                                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
                                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 
                                                                                             at android.app.ActivityThread.access$800(ActivityThread.java:135) 
                                                                                             at android.app.ActivityThread$H.handleMessage

(ActivityThread.java:1196) 
                                                                                             at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                             at android.os.Looper.loop(Looper.java:136) 
                                                                                             at android.app.ActivityThread.main(ActivityThread.java:5017) 
                                                                                             at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                                             at java.lang.reflect.Method.invoke(Method.java:515) 
                                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
                                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
                                                                                             at dalvik.system.NativeStart.main(Native Method) 

Replace: 更换:

Intent intent = new Intent(SensorActivity.this, MainActivity.class);
startActivity(intent);

With: 带有:

finish();

This will close the current Activity and take you back to the main Activity. 这将关闭当前活动,并带您回到主要活动。

To Solve the problem that homeButton is null: Your looking for the Button in the Activity's UI and not of the PopUpWindow. 解决homeButton为null的问题:在Activity的UI中而不是PopUpWindow中寻找Button。

To find it in the PopUpWindow use: 要在PopUpWindow中找到它,请使用:

homeButton = (Button) (popupView1.findViewById(R.id.home_button));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM