【问题标题】:Popup window not showing up with spinner item selection?弹出窗口未显示微调项选择?
【发布时间】:2014-12-31 19:41:25
【问题描述】:

单击微调器项目需要显示弹出窗口但未显示,只有我可以看到 toast 通知。

日志:system.err java.lang.IllegalStateException:系统服务在 onCreate() 之前对活动不可用

下面是我的代码

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

        }
        };

}

微调器事件的监听器。

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_main 布局

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

【问题讨论】:

    标签: java android spinner popupwindow


    【解决方案1】:

    删除您的 CustomOnItemSelectedListener.java 类 然后使用此代码

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

    【讨论】:

      【解决方案2】:

      代替视图的布局传递微调器对象:

      替换

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

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

      要在微调器下方打开弹出窗口,请使用以下代码

      pwindo.showAsDropDown(spinner, 0, 0);
      

      将您的 CustomOnItemSelectedListener 类更新为:

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

      【讨论】:

      • 尝试将布局替换为微调器,但未显示弹出窗口。
      • 添加了activity_main代码,更新了代码。日志:java.lang.IllegalStateException:系统服务在 onCreate() 之前对活动不可用。
      • 'ArrayAdapter dataadapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, array); dataadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(数据适配器); pwindo.showAsDropDown(微调器, 0, 0); addListenerOnSpinnerItemSelected();'在此处添加代码并更新了 CustomOnItemSelectedListener 但应用程序强制关闭出现致命异常错误。空指针异常。 @Anjali
      • 应用程序因该代码崩溃,由 Rakesh Rangani 回答解决了我的问题。谢谢你的回答。