Java 弹出窗口未显示微调器项目选择?

Java 弹出窗口未显示微调器项目选择?,java,android,spinner,popupwindow,Java,Android,Spinner,Popupwindow,单击微调器项目时,需要显示弹出窗口,但它没有显示,只有我可以看到toast通知 日志:system.err java.lang.IllegalStateException:onCreate()之前的活动无法使用系统服务 下面是我的代码 MainActivity.java package com.example.newapplication; import java.util.ArrayList; import android.os.Bundle; import android.app.A

单击微调器项目时,需要显示弹出窗口,但它没有显示,只有我可以看到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();

        }
        };

}
MainActivity.java
包com.example.newapplication;
导入java.util.ArrayList;
导入android.os.Bundle;
导入android.app.Activity;
导入android.content.Context;
导入android.util.Log;
导入android.view.Gravity;
导入android.view.LayoutInflater;
导入android.view.Menu;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.view.ViewGroup;
导入android.widget.ArrayAdapter;
导入android.widget.Button;
导入android.widget.PopupWindow;
导入android.widget.Spinner;
导入com.example.newapplication.service;
公共类MainActivity扩展了活动{
按钮btnClosePopup;
//按钮打开弹出;
数组列表数组;
服务ser=新服务(阵列);
纺纱机;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
微调器=(微调器)findViewById(R.id.spinner1);
/*OpenPopup=(按钮)findViewById(R.id.button1);
setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
//TODO自动生成的方法存根
初始化窗口();
}
});
*/
array=ser.getArrayList();
Log.d(“标记”,“数组”);
//for(int i=0;i arg0){
//TODO自动生成的方法存根
}
}
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>

活动单元主布局图


替换视图的布局过程微调器对象:

替换

            pwindo.showAtLocation(layout, 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

    }

}
公共类CustomOnItemSelectedListener实现OnItemSelectedListener{
上下文mCtx;
公共CustomOnItemSelectedListener(上下文ctx){
this.mCtx=ctx;
}
@凌驾
已选择公共视图(AdapterView父视图、视图、int pos、,
长id){
//TODO自动生成的方法存根
((MainActivity)mCtx.initiatepopupwindow();
Toast.makeText(parent.getContext(),
parent.getItemAtPosition(pos.toString(),Toast.LENGTH\u LONG)
.show();
}
@凌驾
未选择公共无效(AdapterView arg0){
//TODO自动生成的方法存根
}
}

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

        }
    };

}
公共类MainActivity扩展活动{
按钮btnClosePopup;
//按钮打开弹出;
数组列表数组;
服务ser=新服务(阵列);
纺纱机;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
微调器=(微调器)findViewById(R.id.spinner1);
/*
*OpenPopup=(按钮)findViewById(R.id.button1);
*setOnClickListener(新的OnClickListener(){
* 
*@Override public void onClick(视图v){//TODO自动生成方法
*存根初始化窗口();}
* 
* });
*/
array=ser.getArrayList();
Log.d(“标记”,“数组”);
//for(int i=0;i arg0){
//TODO自动生成的方法存根
}
});
}
私人PopupWindow pwindo;
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(R.menu.main,menu);
返回true;
}
公共无效初始化窗口(){
//TODO自动生成的方法存根
试一试{
Log.d(“标签”,popupwindow中的“+”);
LayoutFlater充气器=(LayoutFlater)MainActivity.this
.getSystemService(上下文布局\充气机\服务);
视图布局=充气机。充气(R.layout.Screen弹出,
(视图组)findViewById(R.id.popup_元素);
pwindo=新的PopupWindow(布局,300370,真);
pwindo.显示位置(布局,重心,0,0);
btnClosePopup=(按钮)布局.findViewById(R.id.btn\u关闭\u弹出窗口);
btnClosePopup.setOnClickListener(取消按钮单击侦听器);
}捕获(例外e){
e、 printStackTrace();
}
}
私有OnClickListener取消按钮单击监听器=新建OnClickListener(){
公共void onClick(视图v){
pwindo.disclose();
}
};
}

尝试将布局替换为微调器,但弹出窗口未显示。添加了活动\u主代码,更新了代码。Log:java.lang.IllegalStateException:onCreate()之前的活动无法使用系统服务。'ArrayAdapter dataadapter=new ArrayAdapter(此,android.R.layout.simple_spinner_项,数组);dataadapter.setDropDownViewResource(android.R.layout.simple\u微调器\u下拉项);spinner.setAdapter(dataadapter);pwindo.showas下拉列表(微调器,0,0);addListenerOnSpinnerItemSelected();'在此处添加了代码并更新了CustomOnItemSelectedListener,但应用程序强制关闭获取致命异常错误。空指针异常@Anjaliapp使用该代码崩溃,由Rakesh Rangani回答,解决了我的问题。谢谢你的回答。谢谢你得到了答案。
pwindo.showAsDropDown(spinner, 0, 0);
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

    }

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

        }
    };

}