是否可以在 java 代码中引用 2 个不同的 xml 文件?

Is referring to 2 different xml files in a java code possible?

弹出窗口有一些问题window。在 activity_main.xml 我有一个按钮可以打开一个弹出窗口 window 在 screen.When 我点击它弹出窗口 window opens.I 创建了一个自定义布局(pop_up.xml, PopUp.java) 对于弹出 window.In 那个布局,有一个按钮可以关闭 it.At 开头,我有 2 个不同的 java 文件两个 xml 个文件。在 MainActivity.java 文件中,当我单击弹出窗口中的按钮时,第一个按钮的行为与 expected.But 相同 window 它不是 triggered.So 我试图编写该按钮代码在 MainActivity.But 现在它给我以下错误:

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.access0(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.access0(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);
    }


}

你有这个

 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在hierarchy中查找view

      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);
        }
    });