开发者

Exception when creating DatePickerDialog in android

this is the exception ..

10-15 18:15:00.031: ERROR/AndroidRuntime(9202): FATAL EXCEPTION: main
10-15 18:15:00.031: ERROR/AndroidRuntime(9202): android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@405727b0 is not valid; is your activity running?
10-15 18:15:00.031: ERROR/AndroidRuntime(9202):     at android.view.ViewRoot.setView(ViewRoot.java:527)
10-15 18:15:00.031: ERROR/AndroidRuntime(9202):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
10-15 18:15:00.031: ERROR/AndroidRuntime(9202):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
10-15 18:15:00.031: ERROR/AndroidRuntime(9202):     at android.view.Window$LocalWindowManager.addView(Window.java:424)
10-15 18:15:00.031: ERROR/AndroidRuntime(9202):     at android.app.Dialog.show(Dialog.java:241)
10-15 18:15:00.031: ERROR/AndroidRuntime(9202):     at android.app.DatePickerDialog.show(DatePickerDialog.java:132)
10-15 18:15:00.031: ERROR/AndroidRuntime(9202):     at android.app.Activity.showDialog(Activity.java:2566)
10-15 18:15:00.031: ERROR/AndroidRuntime(9202):     at android.app.Activity.showDialog(Activity.java:2524)
10-15 18:15:00.031: ERROR/AndroidRuntime(9202):     at com.android.uis.DaysActivity$4.onClick(DaysActivity.java:82)
10-15 18:15:00.031: ERROR/AndroidRuntime(9202):     at android.view.View.performClick(View.java:2485)
10-15 18:15:00.031: ERROR/AndroidRuntime(9202):     at android.view.View$PerformClick.run(View.java:9080)
10-15 18:15:00.031: ERROR/AndroidRuntime(9202):     at android.os.Handler.handleCallback(Handler.java:587)
10-15 18:15:00.031: ERROR/AndroidRuntime(9202):     at android.os.Handler.dispatchMessage(Handler.java:92)
10-15 18:15:00.031: ERROR/AndroidRuntime(9202):     at android.os.Looper.loop(Looper.java:130)
10-15 18:15:00.031: ERROR/AndroidRuntime(9202):     at android.app.ActivityThread.main(ActivityThread.java:3683)
10-15 18:15:00.031: ERROR/AndroidRuntime(9202):     at java.lang.reflect.Method.invokeNative(Native Method)
10-15 18:15:00.031: ERROR/AndroidRuntime(9202):     at java.lang.reflect.Method.invoke(Method.java:507)
10-15 18:15:00.031: ERROR/AndroidRuntime(9202):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-15 18:15:00.031: ERROR/AndroidRuntime(9202):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-15 18:15:00.031: ERROR/AndroidRuntime(9202):     at dalvik.system.NativeStart.main(Native Method)

and here is the code for creating the DatePickDoilog

edit

full code of the activity

package com.android.uis;

import java.util.ArrayList;
import java.util.Calendar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast; 
import com.android.classes.Appointment;
import com.android.database.DatabaseHelper;
import com.android.networkservices.TimeTableWebService;

public class DaysActivity extends Activity {

    private ListView lvDay;
    private Button bUpdateTimeTable,bChangeDate;
    private ArrayList<String> items;
    private ArrayList<Appointment> appointments;
    private EfficientAdapter adap;
    private int mYear;
    private int mMonth;
    private int mDay;
    static final int DATE_DIALOG_ID = 0;
    Activity a;

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.day_layout);

        lvDay = (ListView)this.findViewById(R.id.lvDay);
        bUpdateTimeTable = (Button)this.findViewById(R.id.bUpdateTimeTable);    
        bChangeDate = (Button)this.findViewById(R.id.bChangeDate);    
        items = new ArrayList<String>();
        appointments = new ArrayList<Appointment>();

        lvDay.setClickable(true);
        bindList();// to bind list items

        a= this;

        lvDay.setOnItemClickListener(new OnItemClickListener(){

            public void onItemClick(AdapterView<?> parent, View view, int position,long id) { }

        });

        bUpdateTimeTable.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View arg0) {

                DatabaseHelper db = new DatabaseHelper(getApplicationContext());
                // At first delete the previous data then retrieve the new one 
                    db.deteleAllAppointments();
                        appointments.clear();
                                items.clear();
                    TimeTableWebService ttws = new TimeTableWebService();
                                ttws.execute("http://mmu4bader.com/AppointmentJSon/getAppointments1");  
                        ttws.getResult(getApplicationContext());
                    bindList();
            }
        });

        bChangeDate.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View arg0) {

                a.showDialog(DATE_DIALOG_ID);
                            Toast.makeText(getApplicationContext(), "date", Toast.LENGTH_SHORT).show();
            }
        });

        // get the current date
        final Calendar c = Calendar.getInstance();
        mYear = c.get(Calendar.YEAR);    
        mMonth = c.get(Calendar.MONTH);    
        mDay = c.get(Calendar.DAY_OF_MONTH); 

        // display the current date (this method is below)    
        updateDisplay();    
    }

    private DatePickerDialog.OnDateSetListener mDateSetListener =

            new DatePickerDialog.OnDateSetListener() { 

                public void onDateSet(DatePicker view, int year,int monthOfYear, int dayOfMonth) {
                    mYear = year;
                    mMonth = monthOfYear;    
                    mDay = dayOfMonth;    
                    updateDisplay();    
                }   
    };

    @Override
    protected Dialog onCreateDialog(int id) {

        switch (id) {
        case DATE_DIALOG_ID:
            return new DatePickerDialog(this,mDateSetListener,mYear, mMonth, mDay);
        }
        return null;    
    }   

    private void updateDisplay() {

    }

    private void bindList(){

        DatabaseHelper db = new DatabaseHelper(getApplicationContext());
            appointments = db.getAllAppointments();
            Log.d("App from DB",appointments.size()+"");
            for(int i=0;i<appointments.size();i++)
        {
            items.add(String.valueOf(appointments.get(i).mTitle));
            Log.d("title from db",String.valueOf(appointments.get(i).mTitle));
        }    
        adap = new EfficientAdapter(this);    
        adap.notifyDataSetChanged();    
        lvDay.setAdapter(adap);    
    }

    public void onResume() {    
        super.onResume();
    }       

    public class EfficientAdapter extends BaseAdapter implements Filterable{

        LayoutInflater inflater;
            Context context;

        public EfficientAdapter(Context context){
                inflater = LayoutInflater.from(context);
                this.context = context;
            }
            public int getCount() {
                   return items.size();
            }
        public Object getItem(int position) {    
            return items.get(position);    
        }
        public long getItemId(int position) {
                    return 0;    
        }    
        public View getView(final int position, View convertView, ViewGroup parent) {    
            ViewHolder holder;    
            if(convertView == null){    
                convertView = inflater.inflate(R.layout.adaptor_timetable_content, null);
                            holder = new ViewHolder();
                holder.textLineTitle = (TextView)convertView.findViewById(R.id.textLineTitle);
                holder.textLineTime = (TextView)convertView.findViewById(R.id.textLineTime);

                convertView.setOnClickListener(new OnClickListener() {

                    private int pos = position;
                    public void onClick(View v) {
                        /*  Intent i = new    Intent(context,Routing开发者_如何学PythonMapActivity.class);
                                             i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                         i.putExtra("Department", depts.get(pos).toBunlde());
    getApplicationContext().startActivity(i);  
                    }
                        });

                convertView.setTag(holder);
            }
                    else{
                holder = (ViewHolder)convertView.getTag();
            }

            // Bind the data efficiently with the holder.

            try{
                holder.textLineTitle.setText(String.valueOf(appointments.get(position).mTitle)); holder.textLineTime.setText(String.valueOf(appointments.get(position).mStartDate));
            }
                    catch(Exception ex){
            }
                return convertView;    
        }    
        public Filter getFilter() {
                    return null;    
        }    
    }    
    static class ViewHolder {    
        TextView textLineTitle;    
        TextView textLineTime;    
    }
}

day_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:weightSum="1"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
  <LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="horizontal"
  android:weightSum="1"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
<Button 
    android:layout_width="wrap_content"
    android:layout_weight="0.5" 
    android:layout_height="wrap_content"
    android:layout_gravity="right"
    android:text="@string/bChangeDate" 
    android:id="@+id/bChangeDate"></Button>
<Button 
    android:layout_width="wrap_content"
    android:layout_weight="0.5" 
    android:layout_height="wrap_content"
    android:layout_gravity="right"
    android:text="@string/btnUpdateTimeTable" 
    android:id="@+id/bUpdateTimeTable"></Button>
</LinearLayout>
 <ListView 
        android:layout_height="wrap_content" 
        android:layout_width="match_parent" 
        android:id="@+id/lvDay"></ListView>

</LinearLayout>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜