我在弹出窗口中遇到了一些问题。在activity_main.xml中,我有一个按钮,可以在屏幕中间打开一个弹出窗口。当我点击它时弹出窗口打开。我为弹出窗口创建了一个自定义布局(pop_up.xml,PopUp.java)。在该布局中,有一个关闭它的按钮。在开始时,我有两个不同的java文件用于这两个xml文件。在MainActivity.java文件中,第一个按钮的行为与我预期的一样。但是当我点击弹出窗口中的按钮时,它没有被触发。所以我试图在MainActivity中编写那些按钮代码。但现在它给出了我的错误如下:
Process: com.example.android.customcounter, PID: 30350
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.customcounter/com.example.android.customcounter.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.ViewGroup.getChildAt(int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2439)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2499)
at android.app.ActivityThread.access$900(ActivityThread.java:166)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1360)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5468)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.ViewGroup.getChildAt(int)' on a null object reference
at com.example.android.customcounter.MainActivity.onCreate(MainActivity.java:68)
at android.app.Activity.performCreate(Activity.java:6556)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2392)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2499)
at android.app.ActivityThread.access$900(ActivityThread.java:166)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1360)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5468)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
MainActivity.java
package com.example.android.customcounter;
import android.annotation.TargetApi;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
PopupWindow popupWindow;
View layout_MainMenu;
int number = 0;
View popupView;
LayoutInflater layoutInflater;
@TargetApi(23)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
layout_MainMenu = (View) findViewById(R.id.layout);
//Foreground'daki siyah fonu saydam yapar
layout_MainMenu.getForeground().setAlpha(0);
Button eksi = (Button) findViewById(R.id.eksi);
eksi.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
number = number - 1;
display(number);
}
});
Button artı = (Button) findViewById(R.id.artı);
artı.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
number = number + 1;
display(number);
}
});
Button seçenekler = (Button) findViewById(R.id.seçenekler);
seçenekler.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
LayoutInflater layoutInflater = (LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
popupView = layoutInflater.inflate(R.layout.pop_up, null);
popupWindow = new PopupWindow(popupView, 900, 550, true);
popupWindow.setOutsideTouchable(true);
popupWindow.setAnimationStyle(android.R.style.Animation_Dialog);
popupWindow.showAtLocation(popupView, Gravity.CENTER, 0, 0);
layout_MainMenu.getForeground().setAlpha(180);
}
});
View popupView = layoutInflater.inflate(R.layout.pop_up, null);
Button kaydet = (Button) popupView.findViewById(R.id.ayrintilar_kaydet);
kaydet.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
popupWindow.showAsDropDown(layout_MainMenu);
popupWindow.dismiss();
layout_MainMenu.getForeground().setAlpha(0);
}
});
}
private void display(int number) {
TextView sayı = (TextView) findViewById(R.id.number_text_view);
sayı.setText("" + number);
}
}
答案 0 :(得分:0)
你有这个
public class MainActivity extends AppCompatActivity {
....
View popupView; // this is a instance variable
然后你有
Button seçenekler = (Button) findViewById(R.id.seçenekler);
seçenekler.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
LayoutInflater layoutInflater = (LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
// view infalted
popupView = layoutInflater.inflate(R.layout.pop_up, null);
因此,您需要使用popupView查找层次结构中的视图
Button seçenekler = (Button) findViewById(R.id.seçenekler);
seçenekler.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
LayoutInflater layoutInflater = (LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
// view infalted
popupView = layoutInflater.inflate(R.layout.pop_up, null);
Button kaydet = (Button) popupView.findViewById(R.id.ayrintilar_kaydet);
.... // rest of the code for button listener
kaydet.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
popupWindow.showAsDropDown(layout_MainMenu);
popupWindow.dismiss();
layout_MainMenu.getForeground().setAlpha(0);
}
});