0

我基于这个网站http://ericharlow.blogspot.com/2010/09/experience-multiple-android-activities.html用 ActivityGroup 实现了一个 TabActivity (TabHost)

我有两个关于实施这个的问题。

第一个问题,将 TabActivity 与 PreferenceActivity 一起使用,当我单击首选项以创建首选项对话框时发生错误。

08-10 21:55:35.919: E/AndroidRuntime(1415): android.view.WindowManager$BadTokenException: 无法添加窗口 -- 令牌 android.app.LocalActivityManager$LocalActivityRecord@405296f0 无效;您的活动正在运行吗?

我找到了一个解决方案,我应该在我的代码中使用“getParent()”而不是“getApplicationContext()”或“this”,但我不知道在哪里/如何做?我不知道如何用我的 PreferenceActivity 做类似“setContext(getParent())”的事情。

我有一个与此完全相同的问题:TabActivity with ActivityGroup and PreferenceActivity child

这是我的代码:

public class MoreUserProfile extends PreferenceActivity {

@Override
public void onCreate(Bundle savedInstanceState) {        
    super.onCreate(savedInstanceState);        
    addPreferencesFromResource(R.layout.more_userprofile); 
}

在我看来,我认为我应该在对话类中实现一些东西,在 onCreateDialogView() 因为android在进行对话时调用这个方法。我尽力了,但我无法解决我的问题

public class MoreUserProfileDateOfBirthDialog extends DialogPreference {
LinearLayout layout;
SharedPreferences.Editor editor;
    public MoreUserProfileDateOfBirthDialog(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    setDialogLayoutResource(R.layout.more_userprofile_dialog_dateofbirth);
    }

@Override
    protected View onCreateDialogView() {
        // TODO Auto-generated method stub
            // I think I should implement some code here.
        //Activity parent = new Activity();
        //LayoutInflater inflater = (LayoutInflater) parent.getLayoutInflater();
        //layout = (LinearLayout) inflater.inflate(R.layout.more_userprofile_dialog_dateofbirth, null);
    }


protected void onBindDialogView(View view) {

layout = (LinearLayout) view.findViewById(R.id.more_userprofile_dialog_dateofbirth_linearlayout);

    SharedPreferences pref = getSharedPreferences();
    int day = pref.getInt("dayOfBirthday", 1);
    int month = pref.getInt("monthOfBirthday", 1);
    int year = pref.getInt("yearOfBirthday", 2000);

    DatePicker dp = (DatePicker) view.findViewById(R.id.more_userprofile_dialog_choosedateofbirth_datePicker);

     dp.init(year, month-1, day, onDateChangedListener);
    layout.removeAllViews();
  layout.addView(dp);     
    super.onBindDialogView(view);
}

DatePicker.OnDateChangedListener onDateChangedListener = new OnDateChangedListener() {
    public void onDateChanged(DatePicker view, int year, int monthOfYear,
            int dayOfMonth) {
        // TODO Auto-generated method stub
        editor = getEditor();
        editor.putInt("dayOfBirthday", view.getDayOfMonth());
        editor.putInt("monthOfBirthday", view.getMonth()+1);
        editor.putInt("yearOfBirthday", view.getYear());
    }
};
@Override
public void onClick(DialogInterface dialog, int which) {
    // TODO Auto-generated method stub
    super.onClick(dialog, which);
    if(which==DialogInterface.BUTTON_NEGATIVE){
        dialog.dismiss();
    }
    if(which==DialogInterface.BUTTON_POSITIVE){
        editor.commit();
    }

}

}

第二个问题,和上面类似,但是这次是关于 TabActivity 和 ListView Adapter(用于自定义 Listview)。在每个自定义列表视图项目上都有 2 个按钮,“编辑”和“删除”。当我单击其中一个按钮时,会出现对话框。但是在我将 TabActivity 应用到我的应用程序后,按下这些按钮时出现错误。错误代码与上面类似:

08-10 22:13:56.643: E/AndroidRuntime(1427): android.view.WindowManager$BadTokenException: 无法添加窗口 -- 令牌 android.app.LocalActivityManager$LocalActivityRecord@40526c50 无效;您的活动正在运行吗?

这是我的代码:

public class ActivityListviewAdapter extends BaseAdapter {

    private LayoutInflater mInflater;
    private Context context;
    private ArrayList<Activities> listData = new ArrayList<Activities>(); 

    public ActivityListviewAdapter(Context context,ArrayList<Activities> listData,) {
        // TODO Auto-generated constructor stub
        super();
        this.context = context;
        this.mInflater = LayoutInflater.from(context);
        this.listData = listData;
    }

    public int getCount() {
        // TODO Auto-generated method stub
        return listData.size(); //ส่งขนาดของ List ที่เก็บข้อมุลอยู่
    }

    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public View getView(final int position, View convertView, ViewGroup parent) {
    final ActivityRowList activitylist;

        if(convertView == null){
            convertView = mInflater.inflate(R.layout.more_customizedactivity_customlistview, null);
            activitylist = new ActivityRowList();

            activitylist.ActivityName = (TextView) convertView.findViewById(R.id.TextView_More_Customizedactivity_Customlistview_ActivityName);
            activitylist.ActivityDetail = (TextView) convertView.findViewById(R.id.TextView_More_Customizedactivity_Customlistview_ActivityDetail);
            activitylist.ActivityType = (ImageView) convertView.findViewById(R.id.ImageView_Activity_ShowallActivity_Customlistview);   
            activitylist.EditCustomizedButton = (Button) convertView.findViewById(R.id.Button_More_Customizedactivity_Customlistview_Edit);
            activitylist.DeleteCustomizedButton =(Button)convertView.findViewById(R.id.Button_More_Customizedactivity_Customlistview_Delete);

            convertView.setTag(activitylist);
        }else{
            activitylist = (ActivityRowList) convertView.getTag();
        }

        activitylist.ActivityName.setText(listData.get(position).getActivityName());
        activitylist.ActivityDetail.setText("MET: "+listData.get(position).getActivityMET().toString());    
        activitylist.ActivityType.setImageResource(R.drawable.activity_type_customized);


        activitylist.EditCustomizedButton.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    editNewActivityDialog(listData.get(position).getActivityName(), listData.get(position).getActivityMET(),position);
                }
            });
        activitylist.DeleteCustomizedButton.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                confirmDeleteCustomizedDialog(listData.get(position).getActivityName(),listData.get(position));
            }
            });

        return convertView;
    }

    private void confirmDeleteCustomizedDialog(final String ActivityNameInput,final Activities listDataposition){
        // TODO Auto-generated method stub
        AlertDialog alertDialog = new AlertDialog.Builder(mInflater.getContext()).create();
        alertDialog.setTitle("Confirm Delete");
        alertDialog.setMessage("Do you want to delete:\"" + ActivityNameInput +"\"" );
        alertDialog.setIcon(R.drawable.ic_launcher);

        alertDialog.setButton(DialogInterface.BUTTON_POSITIVE,"Confirm", new DialogInterface.OnClickListener() {    
            public void onClick(DialogInterface dialog, int which) {
                // Some codes here
        });
        alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE,"Cancel",new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                dialog.dismiss();
            }
        });
        alertDialog.show();
    }

    private void editNewActivityDialog(final String ActivityNameBefore, float ActivityMETBefore,final int ListPosition) {
        // TODO Auto-generated method stub
        final Dialog dialog = new Dialog(mInflater.getContext());
        dialog.setContentView(R.layout.more_customizedactivity_dialog_addnew);
        final EditText EditText_Addnewactivity_Dialog_ActivityName=(EditText)dialog.findViewById(R.id.EditText_More_Customizedactivity_Addnewactivity_Dialog_ActivityName);
        final EditText EditText_Addnewactivity_Dialog_ActivityMET=(EditText)dialog.findViewById(R.id.EditText_More_Customizedactivity_Addnewactivity_Dialog_ActivityMET);
        final Button Button_More_Customizedactivity_Addnewactivity_Dialog_OK=(Button)dialog.findViewById(R.id.Button_More_Customizedactivity_Addnewactivity_Dialog_OK);
        final Button Button_More_Customizedactivity_Addnewactivity_Dialog_Cancel=(Button)dialog.findViewById(R.id.Button_More_Customizedactivity_Addnewactivity_Dialog_Cancel);
        dialog.setTitle("Edit Activity by User");

        EditText_Addnewactivity_Dialog_ActivityName.setText(ActivityNameBefore);
        EditText_Addnewactivity_Dialog_ActivityMET.setText(ActivityMETBefore+"");
        dialog.show();

        Button_More_Customizedactivity_Addnewactivity_Dialog_OK.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                // TODO Auto-generated method stub
                // Some codes here
        });

        Button_More_Customizedactivity_Addnewactivity_Dialog_Cancel.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                dialog.dismiss();
            }
        });
}
}

根据我的代码:

当我在自定义列表视图上按“编辑”时,代码运行 editNewActivityDialog() 方法并使用以下方法构建 Dialog:

最终对话框 dialog = new Dialog(mInflater.getContext());

(我想我应该把“mInflater.getContext()”改成别的东西,但我不知道。)

当我在自定义列表视图上按“删除”按钮时,代码运行 confirmDeleteCustomizedDialog() 方法并使用以下方法构建 AlertDialog:

AlertDialog alertDialog = new AlertDialog.Builder(mInflater.getContext()).create();

(我想我应该把“mInflater.getContext()”改成别的东西,但我不知道。)

非常感谢提前:)

4

0 回答 0