数据库中的数据未加载到我的android活动上

时间:2014-08-23 19:24:00

标签: android database sqlite listview android-asynctask

我正在制作应用程序来练习一些东西,我无法从数据库加载数据,屏幕仍为空白。我搜索了错误,但我找不到。 有谁能告诉我我的错误在哪里纠正它?

编辑:启动时应用关闭

以下是代码:

public class SeleccionRutina extends Activity {
    private DBAdapter dba=new DBAdapter(this);
    private ArrayList<Rutina> arraydir;
    private ListView lista;
    private AdapterRutina adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_seleccion_rutina);
        this.setTitle(this.getResources().getString(title_activity_seleccion_rutina));

        lista = (ListView) findViewById(R.id.listarutinas);
        arraydir = new ArrayList<Rutina>();
        Rutina rutina;



    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.seleccion_rutina, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }


    private class CargarListview extends AsyncTask <Void,Void,ArrayList<Rutina>>{
        Context context;
        ProgressDialog pDialog;
        Activity activity;

        public CargarListview (Context context,Activity activity){
            this.context=context;
            this.activity=activity;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            pDialog = new ProgressDialog(context);
            pDialog.setMessage(this.context.getResources().getString(R.string.cargando_lista));
            pDialog.setCancelable(true);
            pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            pDialog.show();
        }

        @Override
        protected ArrayList<Rutina> doInBackground(Void... voids) {
            dba=dba.open();
            Cursor rut =dba.getRutinas();

            if (rut.moveToFirst()){
                do{

                    Rutina r = new Rutina(rut.getString(0),rut.getString(1)); 
                    arraydir.add(r);
                }while (rut.moveToNext());
            }

            rut.close();

            if (dba != null) {
                dba.close();
            }
            return arraydir;
        }

        @Override
        protected void onPostExecute(ArrayList<Rutina> rutinas) {
            super.onPostExecute(rutinas);

            adapter = new AdapterRutina(activity,rutinas);

            lista.setAdapter(adapter);

            pDialog.dismiss();
        }
    }


    @Override
    protected void onStart() {
        super.onStart();

        CargarListview refrescar=new CargarListview(this.getApplicationContext(),this);
        refrescar.execute();


        lista.setOnItemClickListener(new AdapterView.OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView<?>adapter, View v, int position,long id){
                Toast.makeText(getBaseContext(),"Click en "+arraydir.get(position).getNombre(),Toast.LENGTH_SHORT).show();

            }

        });

        lista.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener(){
            @Override
            public boolean onItemLongClick(AdapterView<?>adapter, View v, int position,long id){
                Toast.makeText(getBaseContext(),"Click largo en "+arraydir.get(position).getNombre(),Toast.LENGTH_SHORT).show();

                return true;
            }
        });

    }

}

这是日志:

java.lang.RuntimeException: Unable to start activity ComponentInfo{****.SeleccionRutina}: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5001)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
            at android.view.ViewRootImpl.setView(ViewRootImpl.java:536)
            at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:259)
            at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
            at android.app.Dialog.show(Dialog.java:286)
            at com.gymtracker.ang.gymtracker.SeleccionRutina$CargarListview.onPreExecute(SeleccionRutina.java:81)
            at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587)
            at android.os.AsyncTask.execute(AsyncTask.java:535)
            at com.gymtracker.ang.gymtracker.SeleccionRutina.onStart(SeleccionRutina.java:125)
            at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
            at android.app.Activity.performStart(Activity.java:5241)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2157)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5001)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

您似乎无法从任何地方调用您的AsyncTask。

你应该refrescar.execute()(除了它AsyncTask的名字不好)。

编辑: 您不需要将ContextActivity同时放入AsyncTask。因此仅通过Activity就足够了。并替换您创建对话框的代码 - pDialog = new ProgressDialog(context);pDialog = new ProgressDialog(activity);

。{