gmavişşş gmavişşş - 2 months ago 11
Android Question

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

I have some problems with popup window. In the activity_main.xml i have a button to open a popup window in the middle of the screen.When i click on it popup window opens.I have created a custom layout (pop_up.xml, PopUp.java) for the popup window.In that layout, there is a button to close it.At the beginning, i had 2 different java files for these two xml files. In the MainActivity.java file, the 1st button behaves as i expected.But when i click on the button in the popup window it isn't triggered.So i tried to write that buttons code in the MainActivity.But now it gives me the error below:

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


}

Answer

You have this

 public class MainActivity extends AppCompatActivity {
 ....
 View popupView; // this is a instance variable

Then you have

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

So you need to use the popupView to find views in the hierarchy

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