アラート ダイアログ ビルダーのスピナーに問題があり、コンパイル時にエラーが表示されずalertdialog.show()
、カスタム ダイアログを表示するメソッドも呼び出しますが、スピナー アイテムを設定できません。これが私のコードです
public Dialog add_pop_up()
{
AlertDialog.Builder builder = new AlertDialog.Builder(UgJadwal.this);
LayoutInflater inflater = UgJadwal.this.getLayoutInflater();
builder.setView(inflater.inflate(R.layout.addpopup, null));
builder.setIcon(R.drawable.add);
builder.setTitle("Add new schedule");
builder.setPositiveButton(R.string.add, new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int id)
{
day = (Spinner)findViewById(R.id.day);
subject = (EditText)findViewById(R.id.subject);
time = (EditText)findViewById(R.id.time);
location = (Spinner)findViewById(R.id.location);
lecture = (EditText)findViewById(R.id.lecture);
ArrayAdapter<CharSequence> adapterDay = ArrayAdapter.createFromResource(UgJadwal.this, R.array.day, android.R.layout.simple_spinner_item);
adapterDay.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
day.setAdapter(adapterDay);
Toast.makeText(getApplicationContext(), "Added Successfully!", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int id)
{
//
}
});
return builder.create();
}
ログキャット:
03-17 20:14:59.030: E/AndroidRuntime(9889): FATAL EXCEPTION: main
03-17 20:14:59.030: E/AndroidRuntime(9889): java.lang.NullPointerException
03-17 20:14:59.030: E/AndroidRuntime(9889): at com.rk.ug_simplify.UgJadwal$3.onClick(UgJadwal.java:195)
03-17 20:14:59.030: E/AndroidRuntime(9889): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:158)
03-17 20:14:59.030: E/AndroidRuntime(9889): at android.os.Handler.dispatchMessage(Handler.java:99)
03-17 20:14:59.030: E/AndroidRuntime(9889): at android.os.Looper.loop(Looper.java:123)
03-17 20:14:59.030: E/AndroidRuntime(9889): at android.app.ActivityThread.main(ActivityThread.java:4627)
03-17 20:14:59.030: E/AndroidRuntime(9889): at java.lang.reflect.Method.invokeNative(Native Method)
03-17 20:14:59.030: E/AndroidRuntime(9889): at java.lang.reflect.Method.invoke(Method.java:521)
03-17 20:14:59.030: E/AndroidRuntime(9889): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-17 20:14:59.030: E/AndroidRuntime(9889): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-17 20:14:59.030: E/AndroidRuntime(9889): at dalvik.system.NativeStart.main(Native Method)
アップデート!上記は私のlogcatですが、そのエラーはカスタムダイアログの別のコンテキストを指しています。私の過ちを知っている人はいますか?助けてくれてありがとう、ありがとう!
アップデート2!! ビューの追加
public Dialog add_pop_up()
{
AlertDialog.Builder builder = new AlertDialog.Builder(UgJadwal.this);
LayoutInflater inflater = UgJadwal.this.getLayoutInflater();
final View dialogview = inflater.inflate(R.layout.addpopup, null);
builder.setView(dialogview);
builder.setIcon(R.drawable.add);
builder.setTitle("Add new schedule");
builder.setPositiveButton(R.string.add, new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int id)
{
day = (Spinner)dialogview.findViewById(R.id.day);
subject = (EditText)dialogview.findViewById(R.id.subject);
time = (EditText)dialogview.findViewById(R.id.time);
location = (Spinner)dialogview.findViewById(R.id.location);
lecture = (EditText)dialogview.findViewById(R.id.lecture);
ArrayAdapter<CharSequence> adapterDay = ArrayAdapter.createFromResource(UgJadwal.this, R.array.day, android.R.layout.simple_spinner_item);
adapterDay.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
day.setAdapter(adapterDay);
String sub = subject.getText().toString();
Toast.makeText(UgJadwal.this, "Success " + sub, Toast.LENGTH_SHORT).show();
}
});