Popup not showing with pick item selection?

When I click on the spinner, a popup needs to be displayed, but it doesn't show, only I see the toast notification.

Log: system.err java.lang.IllegalStateException: System services are not available for operations before onCreate ()

My code is below

 MainActivity.java

package com.example.newapplication;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.Spinner;
import com.example.newapplication.service;

public class MainActivity extends Activity {
    Button btnClosePopup;
    //Button OpenPopup;
    ArrayList array;
    service ser = new service(array);
    Spinner spinner;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        spinner = (Spinner) findViewById(R.id.spinner1);
        /*OpenPopup = (Button) findViewById(R.id.button1);
        OpenPopup.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                initiatepopupwindow();
            }

        });
*/
        array = ser.getArrayList();
        Log.d("TAG","" +array);
        //for(int i=0;i<array.length();i++){

        //}
        ArrayAdapter<String> dataadapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, array);
        dataadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(dataadapter);
        addListenerOnSpinnerItemSelected();
    }

    private PopupWindow pwindo;

    private void addListenerOnSpinnerItemSelected() {
        // TODO Auto-generated method stub
        spinner.setOnItemSelectedListener(new CustomOnItemSelectedListener());
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public void initiatepopupwindow() {
        // TODO Auto-generated method stub
        try{    
            Log.d("TAG", "" +"Inside popupwindow");
        LayoutInflater inflater = (LayoutInflater) MainActivity.this
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                View layout = inflater.inflate(R.layout.screen_popup,
                (ViewGroup) findViewById(R.id.popup_element));
                pwindo = new PopupWindow(layout, 300, 370, true);
                pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0);
                btnClosePopup = (Button) layout.findViewById(R.id.btn_close_popup);
                btnClosePopup.setOnClickListener(cancel_button_click_listener);
        }
                catch (Exception e) {
                    e.printStackTrace();
                    }
    }

    private OnClickListener cancel_button_click_listener = new OnClickListener() {
        public void onClick(View v) {
        pwindo.dismiss();

        }
        };

}

      

Listener for the spinner event.

CustomOnItemSelectedListener.java

package com.example.newapplication;

import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
import com.example.newapplication.MainActivity;


public class CustomOnItemSelectedListener implements OnItemSelectedListener{
    MainActivity main = new MainActivity(); 
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int pos,
            long id) {
        // TODO Auto-generated method stub
        main.initiatepopupwindow();
        Toast.makeText(parent.getContext(), parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub

    }

}

      

screenpopup.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/popup_element"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#444444"
android:orientation="vertical"
android:padding="10sp" >

<TextView
android:id="@+id/txtView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5sp"
android:text="Hello!" />

<Button
android:id="@+id/btn_close_popup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Close" />

</LinearLayout>

      

Activity_Layout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/lightbackground"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="23dp"
        android:layout_marginTop="32dp"
        android:text="Welcome to MyApp"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="21dp" />

</RelativeLayout>

      

+3


source to share


2 answers


remove the CustomOnItemSelectedListener.java class and then use this code



        public class MainActivity extends Activity {
    Button btnClosePopup;
    // Button OpenPopup;
    ArrayList array;
     service ser = new service(array);
    Spinner spinner;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        spinner = (Spinner) findViewById(R.id.spinner1);
        /*
         * OpenPopup = (Button) findViewById(R.id.button1);
         * OpenPopup.setOnClickListener(new OnClickListener(){
         * 
         * @Override public void onClick(View v) { // TODO Auto-generated method
         * stub initiatepopupwindow(); }
         * 
         * });
         */
         array = ser.getArrayList();

        Log.d("TAG", "" + array);
        // for(int i=0;i<array.length();i++){

        // }
        ArrayAdapter<String> dataadapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, array);
        dataadapter
                .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(dataadapter);
        spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                // TODO Auto-generated method stub
                initiatepopupwindow();
                Toast.makeText(getApplicationContext(), array.get(arg2) + "",
                        Toast.LENGTH_LONG).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

            }
        });
    }

    private PopupWindow pwindo;

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public void initiatepopupwindow() {
        // TODO Auto-generated method stub
        try {
            Log.d("TAG", "" + "Inside popupwindow");
            LayoutInflater inflater = (LayoutInflater) MainActivity.this
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View layout = inflater.inflate(R.layout.screenpopup,
                    (ViewGroup) findViewById(R.id.popup_element));
            pwindo = new PopupWindow(layout, 300, 370, true);
            pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0);
            btnClosePopup = (Button) layout.findViewById(R.id.btn_close_popup);
            btnClosePopup.setOnClickListener(cancel_button_click_listener);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private OnClickListener cancel_button_click_listener = new OnClickListener() {
        public void onClick(View v) {
            pwindo.dismiss();

        }
    };

}

      

+1


source


Instead of the layout, pass the spinner of your view:

replace

            pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0);

      

from

                pwindo.showAtLocation(spinner, Gravity.CENTER, 0, 0);

      



And to open the popup just below your counter use below code

pwindo.showAsDropDown(spinner, 0, 0);

      

Update your CustomOnItemSelectedListener class as follows:

public class CustomOnItemSelectedListener implements OnItemSelectedListener {
    Context mCtx;

    public CustomOnItemSelectedListener(Context ctx) {
        this.mCtx = ctx;
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int pos,
            long id) {
        // TODO Auto-generated method stub
        ((MainActivity) mCtx).initiatepopupwindow();
        Toast.makeText(parent.getContext(),
                parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG)
                .show();
    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub

    }

}

      

-1


source







All Articles