2013-02-07 86 views
7

我想創建延伸的一類AlertDialog.Builder擴展AlertDialog.Builder Android中

public class AlertBuilder extends AlertDialog.Builder { 

    private MultiAdapter mAdapter; 
    private ListView lv; 
    private Activity mActivity; 
    private Context mContext; 
    private CheckedTxtView checkedItem; 

    public AlertBuilder(Context context, Activity activity, final List<CheckedTxtView> mList) { 
     super(context); 
     this.mActivity = activity; 
     this.mContext = context; 

     this.setTitle("Test"); 
     LayoutInflater inflater = activity.getLayoutInflater(); 
     View dialoglayout = inflater.inflate(R.layout.main_filter, null); 
     this.setView(dialoglayout); 

     mAdapter = new MultiAdapter(context, mList); 
     lv = (ListView) dialoglayout.findViewById(R.id.list); 
     lv.setAdapter(mAdapter); 
     lv.setOnItemClickListener(new ItemClick()); 
    } 

    class ItemClick implements OnItemClickListener { 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      checkedItem = mAdapter.mItems.get(position);; 
      mAdapter.notifyDataSetChanged(); 
     } 
    } 

    private class MultiAdapter extends ArrayAdapter<CheckedTxtView> { 

     private List<CheckedTxtView> mItems; 

     public MultiAdapter(Context context, List<CheckedTxtView> mList) { 
      super(context, R.layout.list2, mList); 
      this.mItems = mList; 
     } 

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

      View row; 

      if (convertView == null) { 
       LayoutInflater inflater = mActivity.getLayoutInflater(); 
       row = inflater.inflate(R.layout.list2, parent, false); 
      } else 
       row = convertView; 

      CheckedTextView txt = (CheckedTextView) row.findViewById(R.id.label); 

      CheckedTxtView c = mItems.get(position); 

      System.out.println(c.item); 

      txt.setText(c.item); 
      if(checkedItem != null && checkedItem.item != null) 
       txt.setChecked(c.item.equals(checkedItem.item)); 

      return row; 
     } 
    } 

然後我用它在我的活動:

public class MainActivity extends Activity { 

    private List<CheckedTxtView> mList; 
    private AlertDialog mAlert; 
    private AlertDialog.Builder mAlertBuilder; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     mList = new ArrayList<CheckedTxtView>(); 
     for(int i=0; i<30; i++) { 
      CheckedTxtView c = new CheckedTxtView(); 
      c.item = "item " + i; 
      mList.add(c); 
     } 

     mAlertBuilder = new AlertBuilder(getApplicationContext(), this, mList); 
     mAlert = mAlertBuilder.create(); 

     Button mButton = (Button) this.findViewById(R.id.mButton); 
     mButton.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       mAlert.show(); 
      } 
     }); 
    } 
} 

我在控制檯收到此錯誤:

02-07 13:33:05.641: E/AndroidRuntime(12557): FATAL EXCEPTION: main 
02-07 13:33:05.641: E/AndroidRuntime(12557): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application 
02-07 13:33:05.641: E/AndroidRuntime(12557): at android.view.ViewRoot.setView(ViewRoot.java:566) 
02-07 13:33:05.641: E/AndroidRuntime(12557): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:179) 
02-07 13:33:05.641: E/AndroidRuntime(12557): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91) 
02-07 13:33:05.641: E/AndroidRuntime(12557): at android.app.Dialog.show(Dialog.java:265) 
02-07 13:33:05.641: E/AndroidRuntime(12557): at com.example.testdialog.MainActivity$1.onClick(MainActivity.java:35) 
02-07 13:33:05.641: E/AndroidRuntime(12557): at android.view.View.performClick(View.java:2533) 
02-07 13:33:05.641: E/AndroidRuntime(12557): at android.view.View$PerformClick.run(View.java:9320) 
02-07 13:33:05.641: E/AndroidRuntime(12557): at android.os.Handler.handleCallback(Handler.java:587) 
02-07 13:33:05.641: E/AndroidRuntime(12557): at android.os.Handler.dispatchMessage(Handler.java:92) 
02-07 13:33:05.641: E/AndroidRuntime(12557): at android.os.Looper.loop(Looper.java:150) 
02-07 13:33:05.641: E/AndroidRuntime(12557): at android.app.ActivityThread.main(ActivityThread.java:4385) 
02-07 13:33:05.641: E/AndroidRuntime(12557): at java.lang.reflect.Method.invokeNative(Native Method) 
02-07 13:33:05.641: E/AndroidRuntime(12557): at java.lang.reflect.Method.invoke(Method.java:507) 
02-07 13:33:05.641: E/AndroidRuntime(12557): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849) 
02-07 13:33:05.641: E/AndroidRuntime(12557): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607) 
02-07 13:33:05.641: E/AndroidRuntime(12557): at dalvik.system.NativeStart.main(Native Method) 

我可能會錯過這裏的東西。你可以幫我嗎?

+1

或者只是在更改'super(context);'到'super(activity);'之後嘗試;'因爲您已經將Activity上下文發送到AlertBuilder類 –

回答

2

而不是getApplicationContext()使用此或MainActivity.this。

mAlertBuilder = new AlertBuilder(this, this, mList); 
2

你不應該通過這裏應用的上下文:

mAlertBuilder = new AlertBuilder(getApplicationContext(), this, mList); 

由於活動是背景本身,你應該改變你的構造函數,只需要一paramater - 活動,這可以作爲一個Context對象後來。

+0

您是對的。 +1點。 – Ali