我正在尝试使用PopupWindow
来解释我的应用,但是对于看似简单的事情,我遇到了很多困难。我尝试了各种各样的YouTube视频,看了几乎所有关于它的类似帖子,但我还没有运气。如果有人可以指出我的缺点,那就太棒了。
我的最终目标是PopupWindow与on create方法一起出现,但我现在包含一个Button来启动它,因为所有教程都包含一个。
protected void onCreate(Bundle savedInstanceState) {
//BUILDING THE POP UP WINDOW ON CLICK OF HELP BUTTON
help=(Button)findViewById(R.id.help);
help.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
layoutInflater=(LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View customView= layoutInflater.inflate(R.layout.activity_instructions, null);
popUpWindow=new PopupWindow(
customView,
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
myButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
popUpWindow.dismiss();
}
});
}
});
非常感谢任何帮助。如果我需要提供更多信息,请询问。我是android新手,这是我的第一个大应用程序,所以保持我的希望。
07-30 23:32:07.062 9273-9273/my.myCompany.vinay.diversityformulaapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: my.myCompany.vinay.diversityformulaapplication, PID: 9273
java.lang.RuntimeException: Unable to start activity ComponentInfo{my.myCompany.vinay.diversityformulaapplication/my.myCompany.vinay.diversityformulaapplication.EntryScreen}: 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:2947)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3008)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1650)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6688)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
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 my.myCompany.vinay.diversityformulaapplication.EntryScreen.onCreate(EntryScreen.java:37)
at android.app.Activity.performCreate(Activity.java:6912)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2900)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3008)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1650)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6688)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
答案 0 :(得分:0)
myButton 在此处为空。
您错过了这个
myButton = (Button)customView.findViewById(R.id.myButtonId);