为什么我的应用程序崩溃了?

时间:2014-05-03 15:11:28

标签: android database

每当我包含updateUI()方法时,我的应用程序崩溃了,但是当我排除它时它不会崩溃,请解释我为什么?并且请解释使用ArrayList和ArrayAdapter更新我的ListView的更好方法,因为我&# 39;我对使用SimpleCursorAdapter感到困惑,提前问候......

我的MainActivity

package org.example.tasklist;

 import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class MainActivity extends Activity {

    HelperMethods localhelpermethods;
    HelperMethods.OpenHelper localopenhelper;
    SQLiteDatabase localdatabase;
    ArrayList<String> task_list;
    ListView listView1;
    EditText editText1;
    ArrayAdapter<String> aa;

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

        Context activitycontext = getApplicationContext();

        localhelpermethods = new HelperMethods(activitycontext);

        task_list  = new ArrayList<String>();
        listView1 = (ListView)findViewById(R.id.listView1);
        editText1 = (EditText)findViewById(R.id.editText1);

        Button button1 = (Button)findViewById(R.id.button1);

        aa = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,task_list);
        listView1.setAdapter(aa);       
        updateUI();


        button1.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v) 
            {             
               String task;
               localhelpermethods.open_database();
               task = editText1.getText().toString();
               localhelpermethods.add_task(task);

               editText1.setText("");

               updateUI();

            }
         });//working fine, don't know whether the task is being stored in the database.
    }

    public void updateUI()
    {
        task_list = localhelpermethods.getAllTasks();
        aa = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,task_list);
        listView1 = (ListView)findViewById(R.id.listView1);
        listView1.setAdapter(aa);

        aa.notifyDataSetChanged();
    }




    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

My HelperMethods.java

package org.example.tasklist;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class HelperMethods
{

public static final String DATABASE_NAME = "TASK_DATABASE";
public final String DATABASE_TABLE = "TASKS";
public static final int DATABASE_VERSION = 1;


//Columns in the table.
public final String KEY_ID = "_id";
public final String KEY_TASK = "task";

private static String CREATE_DATABASE = "create table TASKS (_id integer primary key autoincrement," + "task text not null);";



    private final Context localcontext;
    private OpenHelper localopenhelper;
    private SQLiteDatabase localdatabase;

    public HelperMethods(Context ctx)
    {
        this.localcontext = ctx;
        localopenhelper = new OpenHelper(localcontext);
    }//correct.

    //correct
     public class OpenHelper extends SQLiteOpenHelper
     {
         Context openhelpercontext;
          public OpenHelper(Context context)
          {
            super(context,DATABASE_NAME,null,DATABASE_VERSION);
            this.openhelpercontext = context;
          }

          @Override
          public void onCreate(SQLiteDatabase database)
          {
              database.execSQL(CREATE_DATABASE);
          }

          @Override
          public void onUpgrade(SQLiteDatabase database, int oldversion, int newversion)
          {
             if(newversion > oldversion)
             {
                 database.execSQL("DROP TABLE IF EXISTS tasks" );
                 onCreate(database);
             }
          }//correct
       }

    public HelperMethods open_database() throws SQLException
    {
        localdatabase = localopenhelper.getWritableDatabase();
        return this;
    }//correct.

    public void close_database()
    {
        localopenhelper.close();
    }//correct.

    public long add_task(String task)
    {
        ContentValues cv = new ContentValues();
        cv.put(KEY_TASK, task);
        return localdatabase.insert("TASKS", null, cv);
    }//correct

    public ArrayList<String> getAllTasks()
    { 
        ArrayList<String> task_list = new ArrayList<String>();
        localopenhelper.getReadableDatabase();
        String[] columns = new String[] {KEY_TASK};
        Cursor cursor = localdatabase.query(DATABASE_TABLE, columns, null, null, null, null, null, null);
        while(cursor.moveToNext())
        {
            task_list.add(cursor.getString(cursor.getColumnIndex(KEY_TASK)));
        }
        return task_list;
    }//correct but not verified.

    public void deleteAllTasks()
    {
        localdatabase.delete(DATABASE_TABLE, KEY_TASK, null);
    }

}

我的错误logcat

05-03 11:12:29.490: E/AndroidRuntime(2714): FATAL EXCEPTION: main
05-03 11:12:29.490: E/AndroidRuntime(2714): Process: org.example.tasklist, PID: 2714
05-03 11:12:29.490: E/AndroidRuntime(2714): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.example.tasklist/org.example.tasklist.MainActivity}: java.lang.NullPointerException
05-03 11:12:29.490: E/AndroidRuntime(2714):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
05-03 11:12:29.490: E/AndroidRuntime(2714):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
05-03 11:12:29.490: E/AndroidRuntime(2714):     at android.app.ActivityThread.access$700(ActivityThread.java:135)
05-03 11:12:29.490: E/AndroidRuntime(2714):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
05-03 11:12:29.490: E/AndroidRuntime(2714):     at android.os.Handler.dispatchMessage(Handler.java:102)
05-03 11:12:29.490: E/AndroidRuntime(2714):     at android.os.Looper.loop(Looper.java:137)
05-03 11:12:29.490: E/AndroidRuntime(2714):     at android.app.ActivityThread.main(ActivityThread.java:4998)
05-03 11:12:29.490: E/AndroidRuntime(2714):     at java.lang.reflect.Method.invokeNative(Native Method)
05-03 11:12:29.490: E/AndroidRuntime(2714):     at java.lang.reflect.Method.invoke(Method.java:515)
05-03 11:12:29.490: E/AndroidRuntime(2714):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
05-03 11:12:29.490: E/AndroidRuntime(2714):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
05-03 11:12:29.490: E/AndroidRuntime(2714):     at dalvik.system.NativeStart.main(Native Method)
05-03 11:12:29.490: E/AndroidRuntime(2714): Caused by: java.lang.NullPointerException
05-03 11:12:29.490: E/AndroidRuntime(2714):     at org.example.tasklist.HelperMethods.getAllTasks(HelperMethods.java:88)
05-03 11:12:29.490: E/AndroidRuntime(2714):     at org.example.tasklist.MainActivity.updateUI(MainActivity.java:68)
05-03 11:12:29.490: E/AndroidRuntime(2714):     at org.example.tasklist.MainActivity.onCreate(MainActivity.java:45)
05-03 11:12:29.490: E/AndroidRuntime(2714):     at android.app.Activity.performCreate(Activity.java:5243)
05-03 11:12:29.490: E/AndroidRuntime(2714):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-03 11:12:29.490: E/AndroidRuntime(2714):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
05-03 11:12:29.490: E/AndroidRuntime(2714):     ... 11 more

1 个答案:

答案 0 :(得分:0)

从您的logcat ...

05-03 11:12:29.490: E/AndroidRuntime(2714): Caused by: java.lang.NullPointerException
05-03 11:12:29.490: E/AndroidRuntime(2714):     at org.example.tasklist.HelperMethods.getAllTasks(HelperMethods.java:88)

您在NullPointerException课程的第88行获得HelperMethods。查看您的代码,最可能的行是......

while(cursor.moveToNext())

...这表明您的查询为null返回Cursor。尝试拨打Cursor之前,请尝试检查moveToNext()。实施例...

if (cursor != null)
    while (cursor.moveToNext())
    {
        task_list.add(cursor.getString(cursor.getColumnIndex(KEY_TASK)));
    }