2

我的模拟器显示应用程序意外停止的消息。我在我的代码中找不到错误。

这是我的主要课程,名为Database2Activity

package data.base;

import com.example.phone_no.DBhelper;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class Database2Activity extends Activity {
EditText editText;
DBhelper DB;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Button button = (Button)findViewById(R.id.button);
    DB = new DBhelper(Database2Activity.this);
    button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            String rr = editText.getText().toString();

            DB.open();
             DB.adddata("A", rr, "122");
                DB.getAlldata();
                DB.close();

        }
    });


}
}

这是我的第二个 java 类,名为DBhelper

package com.example.phone_no;



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;
import android.util.Log;

public class DBhelper {
public static final String KEY_ID = "id";
public static final String KEY_NAME = "names";

public static final String KEY_PHONE = "phoneno";

private final Context ourContext;
private static final String DATABASE_TABLE = "Contactinfo";
private static final int DATABASE_VERSION = 27;
private static final String DATABASE_NAME = "contactdata.db";
private DbHelper ourHelper;
private SQLiteDatabase ourDatabase;
// end for location
private static class DbHelper extends SQLiteOpenHelper {
    public DbHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub

        /*db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ID
                + " INTEGER PRIMARY KEY, " + KEY_NAME + " TEXT NULL , "
                + KEY_PHONE + " INTEGER NULL);");*/
        db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ID
                + " INTEGER , " + KEY_NAME + " TEXT NULL , "
                + KEY_PHONE + " INTEGER NULL);");

        // string value
        String y = "CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ID
                + " INTEGER PRIMARY KEY  , " + KEY_NAME + " TEXT NULL , "
                + KEY_PHONE + " INTEGER NULL);";

        System.out.println("query" + y);
        Log.d("query", y);






    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
        // TODO Auto-generated method stub
        // TODO Auto-generated method stub
        db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
        onCreate(db);

    }

}

public DBhelper(Context c) {
    ourContext = c;
}
public DBhelper open() throws SQLException 
{
    ourHelper = new DbHelper(ourContext);
    ourDatabase = ourHelper.getWritableDatabase();
    return this;
}

public void close() 
{
    ourHelper.close();
}
public long adddata(String id,String name,String number) 
{
    // TODO Auto-generated method stub
    // add the custom Image Gallery Image Path to Data Base
    ContentValues cv = new ContentValues();
    cv.put(KEY_ID, id);
    cv.put(KEY_NAME, name);
    cv.put(KEY_PHONE, number);
    return ourDatabase.insert(DATABASE_TABLE, null, cv);
}

public void getAlldata() 
{
    Cursor details = null;
    if (ourDatabase.isOpen() == false)

        ourDatabase = ourHelper.getWritableDatabase();
    if (ourDatabase.isOpen()) 
    {
        details = ourDatabase.query(DATABASE_TABLE, null, null, null, null, null, null);
         for(details.moveToFirst();!details.isAfterLast();details.moveToNext())
        {
             String a=details.getString(0);
            String b=details.getString(1);
            String c=details.getString(2);
            System.out.println("id--"+a+"name"+b+"phoneno"+c);
        }



    }


}
public long delete_image(String id) 
  {
    if (ourDatabase.isOpen() == false)
        ourDatabase = ourHelper.getWritableDatabase();
      if (ourDatabase.isOpen()) 
        {
            return ourDatabase.delete(DATABASE_TABLE, KEY_ID + "=" + id, null);
        }
        return 0;
    }



}

这是 logcat 窗口上的错误消息:

03-11 23:20:32.677: E/AndroidRuntime(444): FATAL EXCEPTION: main
03-11 23:20:32.677: E/AndroidRuntime(444): java.lang.NullPointerException
03-11 23:20:32.677: E/AndroidRuntime(444):  at data.base.Database2Activity$1.onClick(Database2Activity.java:27)
03-11 23:20:32.677: E/AndroidRuntime(444):  at android.view.View.performClick(View.java:2408)
03-11 23:20:32.677: E/AndroidRuntime(444):  at android.view.View$PerformClick.run(View.java:8816)
03-11 23:20:32.677: E/AndroidRuntime(444):  at android.os.Handler.handleCallback(Handler.java:587)
03-11 23:20:32.677: E/AndroidRuntime(444):  at android.os.Handler.dispatchMessage(Handler.java:92)
03-11 23:20:32.677: E/AndroidRuntime(444):  at android.os.Looper.loop(Looper.java:123)
03-11 23:20:32.677: E/AndroidRuntime(444):  at android.app.ActivityThread.main(ActivityThread.java:4627)
03-11 23:20:32.677: E/AndroidRuntime(444):  at java.lang.reflect.Method.invokeNative(Native Method)
03-11 23:20:32.677: E/AndroidRuntime(444):  at java.lang.reflect.Method.invoke(Method.java:521)
03-11 23:20:32.677: E/AndroidRuntime(444):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-11 23:20:32.677: E/AndroidRuntime(444):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-11 23:20:32.677: E/AndroidRuntime(444):  at dalvik.system.NativeStart.main(Native Method)
4

2 回答 2

5

你从不要求findViewById()所以editText它留下来null。在尝试使用它之前,您需要这样做:

editText = (EditText) findViewById(R.id.your_edittext_id);//replace with your id
button.setOnClickListener(new OnClickListener() {
  //rest of code
于 2013-03-11T18:11:18.387 回答
0

您必须使用findViewById返回视图通过给定的 id 获取视图。

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    editText = (EditText)findViewById(R.id.editText);

    .....
}
于 2013-03-11T18:11:49.957 回答