2013-03-17 122 views
0

我的快速對話框生成器中的微調器出現問題,編譯時並沒有顯示任何錯誤,我還打電話alertdialog.show()顯示我的自定義對話框,但無法填充我的微調器項目。這裏是我的代碼在android對話框生成器中創建微調適配器

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

的logcat:

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

你有從logcat的堆棧跟蹤? – Trinimon 2013-03-17 13:14:10

回答

0

如果微調是內部Dialog佈局,那麼你將需要使用對話框佈局實例來訪問它。改變你的代碼:

AlertDialog.Builder builder = new AlertDialog.Builder(UgJadwal.this); 
    LayoutInflater inflater = UgJadwal.this.getLayoutInflater(); 

    // inflate layout to View 
    View dialogview = inflater.inflate(R.layout.dialog_layout, null); 

    builder.setView(dialogview); //<< set view for Dialog 

    //....your code here.... 

    builder.setPositiveButton(R.string.add, new DialogInterface.OnClickListener() 
    { 
     @Override 
     public void onClick(DialogInterface dialog, int id) 
     { 
      day = (Spinner)dialogview.findViewById(R.id.day); //<< access Spinner 

      // do same for other Views to access it from dialogview 
      // your code here... 
+0

以及我試過你的代碼,但編譯器提示我將'dialogview'的修飾符改爲'final'。我做到了,但仍然無法正常工作。 – 2013-03-17 13:34:39

+0

@RadityaKurnianto:你也需要改變其他視圖,你想從對話框EditText,TextView的... – 2013-03-17 13:37:54

+0

@RadityaKurnianto:哪個錯誤你得到PLZ編輯你的問題與最新的代碼和logcat結果因爲它是可能的你有當前代碼中的其他問題。 – 2013-03-17 13:39:00

0
public void Getradius() { 
    // Set up download interaction. Display a dialog 
    // when the user clicks download button and require 
    // a user-provided region name 
    android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(class.this); 

    final Spinner min_zoom=new Spinner(class.this); 
    final Spinner max_zoom=new Spinner(class.this); 


    ArrayAdapter min = new ArrayAdapter(this, R.layout.spinner_item_register, minzoom_values); 
    min.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
    min_zoom.setAdapter(min); 

    // Build the dialog box 
    builder.setTitle("Set Radius") 
      .setView(min_zoom) 
      .setPositiveButton("GET COURSE LIST", new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int which) { 

       } 
      }) 
      .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int which) { 
        dialog.cancel(); 
       } 
      }); 
    // Display the dialog 
    builder.show(); 
}