0

我的应用程序有一个 MainActivity 和一个 Database Helpler 类。

主要活动

package com.example.sample;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.os.Bundle;
import android.app.Activity;
import android.database.sqlite.SQLiteDatabase;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity{


Button b1;
EditText et1;
protected DatabaseHelper helper;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    helper=new DatabaseHelper(this);
    b1=(Button)findViewById(R.id.button1);
    et1=(EditText)findViewById(R.id.editText1);

    b1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub



    SimpleDateFormat formatter=new SimpleDateFormat("dd/MM/yyyy");
    Date dateobject;

    String date_var=(et1.getText().toString());
    try {
        dateobject=formatter.parse(date_var);
        String date=new SimpleDateFormat().format(dateobject);
        helper.AddDetail(date);
Toast.makeText(getApplicationContext(),""+date, Toast.LENGTH_SHORT).show();    
        helper.close();
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

        }
    });

}



}

DatabaseHelper 类定义如下

 package com.example.sample;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHelper extends SQLiteOpenHelper {

DatabaseHelper helper;
SQLiteDatabase db;
static final int VERSION=2;
static final String DATA_NAME="Mydata";
Context c;

public DatabaseHelper(Context context) {
    super(context, DATA_NAME, null, VERSION);
    c=context;
    // TODO Auto-generated constructor stub
}

@Override
public void onCreate(SQLiteDatabase arg0) {
    // TODO Auto-generated method stub
    String query="create table if not exists Mytable(id Integer primary key,Daate text 
    not null)";
    db.execSQL(query);
}
public long AddDetail(String date)
{
    db=getWritableDatabase();
    ContentValues vals=new ContentValues();
    vals.put("Daate",date);
    long a=db.insert("Mytable", null, vals);
    db.close();
    return a;

}

@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
    // TODO Auto-generated method stub

}

    }

它在db.execSQL(query);行显示错误。在数据库助手类(定义如下)和行helper.AddDetail(date); 在 MainActivity 中。Logcat 如下图所示

     09-03 16:51:22.481: E/AndroidRuntime(3240): FATAL EXCEPTION: main
     09-03 16:51:22.481: E/AndroidRuntime(3240): java.lang.NullPointerException
     09-03 16:51:22.481: E/AndroidRuntime(3240):    at                                                              com.example.sample.DatabaseHelper.onCreate(DatabaseHelper.java:27)
     09-03 16:51:22.481: E/AndroidRuntime(3240):    at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
     09-03 16:51:22.481: E/AndroidRuntime(3240):    at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
     09-03 16:51:22.481: E/AndroidRuntime(3240):    at com.example.sample.DatabaseHelper.AddDetail(DatabaseHelper.java:31)
     09-03 16:51:22.481: E/AndroidRuntime(3240):    at com.example.sample.MainActivity$1.onClick(MainActivity.java:48)
     09-03 16:51:22.481: E/AndroidRuntime(3240):    at android.view.View.performClick(View.java:4204)
     09-03 16:51:22.481: E/AndroidRuntime(3240):    at android.view.View$PerformClick.run(View.java:17355)
     09-03 16:51:22.481: E/AndroidRuntime(3240):    at android.os.Handler.handleCallback(Handler.java:725)
     09-03 16:51:22.481: E/AndroidRuntime(3240):    at android.os.Handler.dispatchMessage(Handler.java:92)
     09-03 16:51:22.481: E/AndroidRuntime(3240):    at android.os.Looper.loop(Looper.java:137)
     09-03 16:51:22.481: E/AndroidRuntime(3240):    at android.app.ActivityThread.main(ActivityThread.java:5041)
     09-03 16:51:22.481: E/AndroidRuntime(3240):    at java.lang.reflect.Method.invokeNative(Native Method)
     09-03 16:51:22.481: E/AndroidRuntime(3240):    at java.lang.reflect.Method.invoke(Method.java:511)
     09-03 16:51:22.481: E/AndroidRuntime(3240):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
     09-03 16:51:22.481: E/AndroidRuntime(3240):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
     09-03 16:51:22.481: E/AndroidRuntime(3240):    at dalvik.system.NativeStart.main(Native Method)
4

3 回答 3

2

更改此行,mm 表示分钟,MM 表示月

SimpleDateFormat formatter=new SimpleDateFormat("dd/MM/yyyy");

您需要在 oncreate 方法中创建 DatabaseHelper 的对象

helper=new DatabaseHelper(parameters);
于 2013-09-03T09:42:26.437 回答
1

你犯了愚蠢的错误:

改变

 SimpleDateFormat formatter=new SimpleDateFormat("dd/mm/yyyy");

 SimpleDateFormat formatter=new SimpleDateFormat("dd/MM/yyyy");

只需在 onCreate() 方法之后添加这一行:因为您刚刚声明了 varible..但没有将其作为对象..

helper = DatabaseHelper(your parameters here);
于 2013-09-03T09:45:58.607 回答
0

我解决了你的问题。如果您不能完全理解我的解释,请参阅这个精彩的教程。DatabaseHelper 类不应该是一个类,它应该是另一个类的子类。我对你的代码做了很多修改。只需像我写的那样更改您的代码。说得够多了,让我们来看代码。

MainActivity.java中的更改很少,我已对所有更改进行了评论

package com.example.sample; 

import java.text.ParseException; 
import java.text.SimpleDateFormat; 
import java.util.Date; 

import android.os.Bundle; 
import android.app.Activity; 
import android.database.sqlite.SQLiteDatabase; 
import android.text.format.DateFormat; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 

public class MainActivity extends Activity {

    Button b1;
    EditText et1;
    protected DatabaseAdapter helper; // type of variable helper has been changed

    @Override 
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        helper = new DatabaseAdapter(this); //the context of the current class is passed
        helper.open();
        b1 = (Button)findViewById(R.id.button1);
        et1 = (EditText)findViewById(R.id.editText1);

        b1.setOnClickListener(new OnClickListener() {

            @Override 
            public void onClick(View v) {
                // TODO Auto-generated method stub 

                SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
                Date dateobject;

                String date_var=(et1.getText().toString());
                try { 
                    dateobject=formatter.parse(date_var);
                    String date=new SimpleDateFormat().format(dateobject);
                    helper.AddDetail(date);
                    Toast.makeText(getApplicationContext(),""+date, Toast.LENGTH_SHORT).show();    
                    helper.close();
                } catch (ParseException e) {
                    // TODO Auto-generated catch block 
                    e.printStackTrace();
                } 
            } 
        });  
    } 
} 

现在来到DatabaseHelper.java我已经把它变成另一个类DatabaseAdapter.java的子类

package com.example.sample;

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

public class DatabaseAdapter {

    DatabaseHelper helper;
    SQLiteDatabase db;
    static final int VERSION=2;
    static final String DATA_NAME="Mydata";
    private static final String query =  "create table Mytable(id Integer primary key,Daate text not null)";  //the query has been changed
    private final Context mCtx;


     public DatabaseAdapter(Context ctx) {
            this.mCtx = ctx;
        }

            //DatabaseHelper has become a subclass
    public class DatabaseHelper extends SQLiteOpenHelper {

        public DatabaseHelper(Context context) {
            super(context, DATA_NAME, null, VERSION);
                        // TODO Auto-generated constructor stub 
        } 

        @Override 
        public void onCreate(SQLiteDatabase arg0) {
            // TODO Auto-generated method stub 
            arg0.execSQL(query); 
        } 

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // TODO Auto-generated method stub
        }
    }

     public DatabaseAdapter open() throws SQLException {
         helper = new DatabaseHelper(mCtx);
            db=helper.getWritableDatabase();
            return this;
        }

      public void close() {
            helper.close();
        }

    public long AddDetail(String date)
    { 
        Log.d("tag",date);
        ContentValues vals=new ContentValues();
        vals.put("Daate",date);
        long a=db.insert("Mytable", null, vals);
        return a;
    } 
}
于 2013-09-04T12:36:04.430 回答