0

I implement a TabActivity (TabHost) with ActivityGroup based on this website http://ericharlow.blogspot.com/2010/09/experience-multiple-android-activities.html

I have 2 questions about implement this.

First question, Using TabActivity with PreferenceActivity, error occurs when I click an preference item to make a preference dialog.

08-10 21:55:35.919: E/AndroidRuntime(1415): android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@405296f0 is not valid; is your activity running?

I found a solution that I should use "getParent()" instead of "getApplicationContext()"or"this" in my code, but I don't know where/how to do it? I don't know how to do something like "setContext(getParent())" with my PreferenceActivity.

I have a problem exactly same as this: TabActivity with ActivityGroup and PreferenceActivity child

Here is my code:

public class MoreUserProfile extends PreferenceActivity {

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

In my opinion, I think I should implement something in a dialog class, in onCreateDialogView() because android calls this method at making dialog time. I try my best but I can't solve my problem

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

}

}

Second question, Similar as Above, but this time is about TabActivity with ListView Adapter (Use for Custom Listview). On each Custom Listview item has 2 buttons, "Edit" and "Delete". When I click on one of these button, dialog appears. But after I apply TabActivity to my application, I got an error when press these button. The error code is similar as above:

08-10 22:13:56.643: E/AndroidRuntime(1427): android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@40526c50 is not valid; is your activity running?

Here is my code:

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

According to my code:

When I press "Edit" on my custom listview, code runs editNewActivityDialog() method and builds Dialog by using this:

final Dialog dialog = new Dialog(mInflater.getContext());

(I think I should change "mInflater.getContext()" to something else but I don't know.)

When I press "Delete" button on my custom listview, code runs confirmDeleteCustomizedDialog() method and builds AlertDialog by using this:

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

(I think I should change "mInflater.getContext()" to something else but I don't know.)

Very thanks in advance :)

Community
  • 1
  • 1
Coco Tan
  • 47
  • 1
  • 8
  • Since your question contains multiple questions and forces people to scroll down the page a few times to read it, i would suggest breaking this up into multiple questions. Ask the first one, and then when you are satisfied with the answer, ask the other one. This is a huge wall of text and many people are just going to skip it. – prolink007 Aug 10 '12 at 16:00
  • As per @prolink007 's advice, it would be better to ask just one question at a time. – Kev Aug 10 '12 at 23:19

0 Answers0