我有主屏幕,点击菜单时有一个名为菜单的按钮我打开一个自定义对话框,其中有一个微调器 我想通过使用数组来填充它 一切正常,但是当我为旋转器设置适配器时,它显示错误
这就是我在android中做的事情,
package com.example.drydays;
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Spinner;
public class MainActivity extends Activity {
final Context context = this;
private Button button;
private String[] states = new String[]{
"Gujrat","Jammu and Kashmir","Kerala","Karnataka","Lakshadweep","Maharashtra","Manipur","Mizoram",
"Nagaland","New Delhi","Rajasthan","Tami Nadu","West Bengal"
};
ArrayList<String> categoryList = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//make it fullscreen
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
//fix portrait orientation
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.activity_main);
ImageView img = (ImageView)findViewById(R.id.imageView1);
img.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// custom dialog
final Dialog dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.menu);
Button dialogButton = (Button) dialog.findViewById(R.id.btncross);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
});
Spinner spin = (Spinner)findViewById(R.id.spinState);
ArrayAdapter<String> adapter_state = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, states);
adapter_state.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
**spin.setAdapter(adapter_state);**
// adding this line gives me error
}
}
答案 0 :(得分:3)
根据您的解释,您似乎在menu.xml
中有一个微调器。
所以你需要进入onClick
Spinner spin = (Spinner)dialog.findViewById(R.id.spinState);
ArrayAdapter<String> adapter_state = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, states);
adapter_state.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(adapter_state);
同时更改此
final Dialog dialog = new Dialog(context);
// context is initialize before onCreate. its null
到
final Dialog dialog = new Dialog(MainActivity.this);
答案 1 :(得分:2)
试试这个希望这会对你有所帮助
Spinner spin = (Spinner)dialog.findViewById(R.id.spinState);
答案 2 :(得分:1)
final Dialog dialog = new Dialog(MainActivity.this);
然后
Spinner spin = (Spinner)dialog.findViewById(R.id.spinState);