我的问题是,如果我能够使用通过弹出窗口生成的按钮来引用其他活动。当我尝试使用intent时,它给了我一个空指针错误。
这是我的虚拟类的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);
}
});}
}
我希望弹出窗口生成的按钮能够将我带到主要活动。
任何指针都会有所帮助!
编辑: 我的意图是:
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);
}
});}
错误的相应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)
答案 0 :(得分:0)
替换:
Intent intent = new Intent(SensorActivity.this, MainActivity.class);
startActivity(intent);
使用:
finish();
这将关闭当前活动,并将您带回主活动。
解决homeButton为null的问题: 您在Activity的UI中寻找Button而不是PopUpWindow。
要在PopUpWindow中找到它,请使用:
homeButton = (Button) (popupView1.findViewById(R.id.home_button));