2013-10-10 38 views
0

我無法顯示對話框,我嘗試但返回空指針。 這個問題,我認爲是上下文,我已經嘗試過,但獲得相同的結果... 另一種可能性,我認爲,是打開一個線程和主要活動內顯示AlertDialog,這是我的代碼:Alertdialog無法在getview上顯示

文件Sector1.java 公共類扇區1延伸片段 {

private Button btnNew_order,btnAggiungi; 
    private Spinner spin_prodotto, spin_tipo; 
    private EditText EtQta; 
    ListView userList; 
    static UserCustomAdapter userAdapter; 
    ArrayList<User> userArray = new ArrayList<User>(); 


     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
      View rootView = inflater.inflate(R.layout.sector1, container, false); 

      userAdapter = new UserCustomAdapter(getActivity().getBaseContext(), R.layout.row, userArray); 
      userList = (ListView) rootView.findViewById(R.id.listView); 
      userList.setItemsCanFocus(false); 
      userAdapter.notifyDataSetChanged(); 
      userList.setAdapter(userAdapter); 

UserCustomAdapter.java

public class UserCustomAdapter extends ArrayAdapter<User> { 
    private static final int INVISIBLE = 4; 
    private static final int VISIBLE = 0; 
    Context context; 
    int layoutResourceId; 
    ArrayList<User> data = new ArrayList<User>(); 

    public UserCustomAdapter(Context context, int layoutResourceId, ArrayList<User> data) { 
        super(context, layoutResourceId, data); 
        this.layoutResourceId = layoutResourceId; 
        this.context = context; 
        this.data = data; 
      } 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 

      View row = convertView; 
      UserHolder holder = null; 

      System.out.println("@@@[email protected]@@"); 
      if (row == null) { 

       //LayoutInflater inflater = ((Activity) context).getLayoutInflater(); 
       LayoutInflater inflater = LayoutInflater.from(context); 
       row = inflater.inflate(layoutResourceId, parent, false); 
       holder = new UserHolder(); 
       holder.textName = (TextView) row.findViewById(R.id.tv_prodotto); 
       holder.textQta = (TextView) row.findViewById(R.id.tv_qta); 
       //holder.textLocation = (TextView) row.findViewById(R.id.textView3); 
       holder.btnEdit = (ImageButton) row.findViewById(R.id.button1); 
       holder.btnDelete = (ImageButton) row.findViewById(R.id.button2); 

      row.setTag(holder); 
      } else { 
      holder = (UserHolder) row.getTag(); 
      } 

      User user = data.get(position); 
      holder.Useritem = data.get(position); 
      holder.textName.setText(user.getName()); 
      holder.textQta.setText(user.getQta()); 
      //holder.textLocation.setText(user.getLocation()); 

      holder.btnDelete.setTag(holder.Useritem);// mi segno la posizione in modo da recuperarla poi in seguito!! 
      holder.btnEdit.setTag(holder.Useritem);// mi segno la posizione in modo da recuperarla poi in seguito!! 

      //rendo invisibili i pulsanti per la prima riga 
      if(position == 0){ 
       holder.btnDelete.setVisibility(INVISIBLE); 
       holder.btnEdit.setVisibility(INVISIBLE); 
      }else{ 
       holder.btnDelete.setVisibility(VISIBLE); 
       holder.btnEdit.setVisibility(VISIBLE); 
      } 
      holder.btnEdit.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
      // TODO Auto-generated method stub 
      Log.i("Edit Button Clicked", "**********"); 
      showDialog(); 
      } 
      }); 

      holder.btnDelete.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       Log.i("Delete Button Clicked", "**********"); 
       Toast.makeText(context, "Delete item", 
        Toast.LENGTH_LONG).show(); 

       User itemToRemove = (User)v.getTag(); 
       Sector1.userAdapter.remove(itemToRemove); 
       Sector1.userAdapter.notifyDataSetChanged(); 
      } 
      }); 
      return row; 
    } 

    private void showDialog(){ 
      AlertDialog.Builder alertDialog = new AlertDialog.Builder(context); 
      AlertDialog dialog = alertDialog.create();  
      alertDialog.setTitle("Conformation"); 
      alertDialog.setMessage("Are you sure you want to do ???"); 
      alertDialog.setIcon(android.R.drawable.ic_dialog_alert); 
      alertDialog.show(); 
     } 

     static class UserHolder { 
      TextView textName; 
      TextView textQta; 
      //TextView textLocation; 
      ImageButton btnEdit; 
      ImageButton btnDelete; 
      User Useritem; 
     } 
    } 

NoticeDialogFragment.java

公共類NoticeDialogFragment上MainActivity.java延伸DialogFragment {

/* The activity that creates an instance of this dialog fragment must 
* implement this interface in order to receive event callbacks. 
* Each method passes the DialogFragment in case the host needs to query it. */ 
public interface NoticeDialogListener { 
    public void onDialogPositiveClick(DialogFragment dialog); 
    public void onDialogNegativeClick(DialogFragment dialog); 
} 

// Use this instance of the interface to deliver action events 
NoticeDialogListener mListener; 

// Override the Fragment.onAttach() method to instantiate the NoticeDialogListener 
@Override 
public void onAttach(Activity activity) { 
    super.onAttach(activity); 
    // Verify that the host activity implements the callback interface 
    try { 
     // Instantiate the NoticeDialogListener so we can send events to the host 
     mListener = (NoticeDialogListener) activity; 
    } catch (ClassCastException e) { 
     // The activity doesn't implement the interface, throw exception 
     throw new ClassCastException(activity.toString() 
       + " must implement NoticeDialogListener"); 
    } 
} 

@Override 
public Dialog onCreateDialog(Bundle savedInstanceState) { 
    // Build the dialog and set up the button click handlers 
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
    builder.setMessage("ciaoo") 
      .setPositiveButton("ok", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        // Send the positive button event back to the host activity 
        mListener.onDialogPositiveClick(NoticeDialogFragment.this); 
       } 
      }) 
      .setNegativeButton("cancel", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        // Send the negative button event back to the host activity 
        mListener.onDialogPositiveClick(NoticeDialogFragment.this); 
       } 
      }); 
    return builder.create(); 
} 

}

public void showNoticeDialog() { 
      // Create an instance of the dialog fragment and show it 
      DialogFragment dialog = new NoticeDialogFragment(); 
      dialog.show(getSupportFragmentManager(), "NoticeDialogFragment"); 
     } 
// The dialog fragment receives a reference to this Activity through the 
    // Fragment.onAttach() callback, which it uses to call the following methods 
    // defined by the NoticeDialogFragment.NoticeDialogListener interface 
    @Override 
    public void onDialogPositiveClick(DialogFragment dialog) { 
     // User touched the dialog's positive button 
     Toast.makeText(this, "Positive button Clicked", 
      Toast.LENGTH_LONG).show(); 
    } 

    @Override 
    public void onDialogNegativeClick(DialogFragment dialog) { 
     // User touched the dialog's negative button 
     Toast.makeText(this, "Negative button Clicked", 
        Toast.LENGTH_LONG).show();   
    } 

竟被我d喜歡通過調用showNoticeDialog()來實現Dialog的第二個版本,如果我在MainActivity上調用此函數一切正常,但是如何在上面的getView上調用此函數?

這是我的第一個對話框梅託德LOG:

10-10 19:41:49.206: D/MainActivity(9150): MainActivity.onCreate 
10-10 19:41:49.218: D/MainActivity(9150): MainActivity.onStart 
10-10 19:41:49.218: D/MainActivity(9150): MainActivity.onResume 
10-10 19:41:49.278: W/EGL_emulation(9150): eglSurfaceAttrib not implemented 
.... 
10-10 19:41:52.914: I/Edit Button Clicked(9150): ********** 
10-10 19:41:52.914: D/AndroidRuntime(9150): Shutting down VM 
10-10 19:41:52.918: W/dalvikvm(9150): threadid=1: thread exiting with uncaught exception (group=0xa622c288) 
10-10 19:41:52.918: E/AndroidRuntime(9150): FATAL EXCEPTION: main 
10-10 19:41:52.918: E/AndroidRuntime(9150): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application 
10-10 19:41:52.918: E/AndroidRuntime(9150):  at android.view.ViewRootImpl.setView(ViewRootImpl.java:589) 
10-10 19:41:52.918: E/AndroidRuntime(9150):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:326) 
10-10 19:41:52.918: E/AndroidRuntime(9150):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224) 
10-10 19:41:52.918: E/AndroidRuntime(9150):  at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149) 
10-10 19:41:52.918: E/AndroidRuntime(9150):  at android.app.Dialog.show(Dialog.java:277) 
10-10 19:41:52.918: E/AndroidRuntime(9150):  at android.app.AlertDialog$Builder.show(AlertDialog.java:932) 
10-10 19:41:52.918: E/AndroidRuntime(9150):  at com.bandweb.ordinifornitori.UserCustomAdapter.showDialog(UserCustomAdapter.java:148) 
10-10 19:41:52.918: E/AndroidRuntime(9150):  at com.bandweb.ordinifornitori.UserCustomAdapter.access$0(UserCustomAdapter.java:142) 
10-10 19:41:52.918: E/AndroidRuntime(9150):  at com.bandweb.ordinifornitori.UserCustomAdapter$1.onClick(UserCustomAdapter.java:121) 
10-10 19:41:52.918: E/AndroidRuntime(9150):  at android.view.View.performClick(View.java:4084) 
10-10 19:41:52.918: E/AndroidRuntime(9150):  at android.view.View$PerformClick.run(View.java:16966) 
10-10 19:41:52.918: E/AndroidRuntime(9150):  at android.os.Handler.handleCallback(Handler.java:615) 
10-10 19:41:52.918: E/AndroidRuntime(9150):  at android.os.Handler.dispatchMessage(Handler.java:92) 
10-10 19:41:52.918: E/AndroidRuntime(9150):  at android.os.Looper.loop(Looper.java:137) 
10-10 19:41:52.918: E/AndroidRuntime(9150):  at android.app.ActivityThread.main(ActivityThread.java:4745) 
10-10 19:41:52.918: E/AndroidRuntime(9150):  at java.lang.reflect.Method.invokeNative(Native Method) 
10-10 19:41:52.918: E/AndroidRuntime(9150):  at java.lang.reflect.Method.invoke(Method.java:511) 
10-10 19:41:52.918: E/AndroidRuntime(9150):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
10-10 19:41:52.918: E/AndroidRuntime(9150):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
10-10 19:41:52.918: E/AndroidRuntime(9150):  at dalvik.system.NativeStart.main(Native Method) 
+1

你在構造函數中傳遞什麼上下文? applicationContext? – Shubhank

+0

首先,你必須更清楚你想做什麼。你想在什麼情況下實現什麼任務?在你的描述中你提到了一個關於Dialog的東西,但是在代碼中你向我們展示了一個ArrayAdapter。你想展示什麼對話框,爲什麼?你打算如何使用該適配器?同時顯示錯誤日誌的完整跟蹤 - 可能會有一個「由...引起」的地方,這是有用的。 – trungdinhtrong

+0

我已更新代碼 –

回答

1

你嘗試使用它的情況下發送的活動?

另一方面,你應該提取一個類似下面的方法。

private void showDialog(){ 
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(context); 
    AlertDialog dialog = alertDialog.create();  
    alertDialog.setTitle("Conformation"); 
    alertDialog.setMessage("Are you sure you want to do ???"); 
    alertDialog.setIcon(android.R.drawable.ic_dialog_alert); 
    alertDialog.show(); 
} 
+0

同樣的例外問題... –

0

您必須更換

alertDialog.setTitle("Conformation"); 
    alertDialog.setMessage("Are you sure you want to do ???"); 
    alertDialog.setIcon(android.R.drawable.ic_dialog_alert); 
    alertDialog.show(); 

dialog.setTitle("Conformation"); 
     dialog.setMessage("Are you sure you want to do ???"); 
     dialog.setIcon(android.R.drawable.ic_dialog_alert); 
     dialog.show(); 
5

你沒有表現出alertdialog呢。試試吧

AlertDialog dialog; 
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);  
    alertDialog.setTitle("Conformation"); 
    alertDialog.setMessage("Are you sure you want to do ???"); 
    alertDialog.setIcon(android.R.drawable.ic_dialog_alert); 
    dialog = alertDialog .create(); 
    dialog .show(); 

你只需要顯示alertdialog,而不是建設者。在這裏你使用通過你的構造函數初始化的完美上下文。開心編程..

+0

同樣的異常問題... –

+0

註釋句子sysyem.out.println(),然後編譯它 – Ranjit

+0

並且還改變layoutInflater初始化類似於LayoutInflater inflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); – Ranjit