【问题标题】:Android Application keeps on crashing, Fatal Exception MainAndroid应用程序不断崩溃,致命异常主要
【发布时间】:2014-03-20 07:43:12
【问题描述】:

我正在制作一个笔记应用程序,我在其中做笔记,它被存储在 Database 中,我可以编辑和删除它,所以,我正要继续我的编辑和删除冒险,我以为我会运行应用程序,看看它是否还好,它刚刚崩溃了。

数据库处理程序

  package com.example.quicknotetaker;

  import java.util.ArrayList;

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

 public class DatabaseHandler extends SQLiteOpenHelper {

// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;

// Database Name
private static final String DATABASE_NAME = "Qdatabase";

// Contacts table name
private static final String DATABASE_TABLE = "qtable";

// Contacts Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_TITLE = "title";
private static final String KEY_NOTE = "note";
private final ArrayList<Editablegetset> titlenoteslist = new ArrayList<Editablegetset>();

public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_DATABASE_TABLE = "CREATE TABLE " + DATABASE_TABLE + "("
    + KEY_ID + " INTEGER PRIMARY KEY," + KEY_TITLE + " TEXT,"
    + KEY_NOTE + " TEXT,)";
db.execSQL(CREATE_DATABASE_TABLE);
}

// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);

// Create tables again
onCreate(db);
}

/**
 * All CRUD(Create, Read, Update, Delete) Operations
 */

// Adding new contact
public void Add_titlenotes(Editablegetset editablegetset) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_TITLE, editablegetset.getTitle()); // Contact Name
values.put(KEY_NOTE, editablegetset.getNote()); // Contact Phone

// Inserting Row
db.insert(DATABASE_TABLE, null, values);
db.close(); // Closing database connection
}

// Getting single data
Editablegetset Get_Set(int id) {
SQLiteDatabase db = this.getReadableDatabase();

Cursor cursor = db.query(DATABASE_TABLE, new String[] { KEY_ID,
    KEY_TITLE, KEY_NOTE}, KEY_ID + "=?",
    new String[] { String.valueOf(id) }, null, null, null);
if (cursor != null)
    cursor.moveToFirst();

Editablegetset editablegetset = new Editablegetset(Integer.parseInt(cursor.getString(0)),
    cursor.getString(1), cursor.getString(2));
// return contact
cursor.close();
db.close();

return editablegetset;
}

// Getting All Contacts
public ArrayList<Editablegetset> Get_Set() {
try {
    titlenoteslist.clear();

    // Select All Query
    String selectQuery = "SELECT  * FROM " + DATABASE_TABLE;

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
    do {
        Editablegetset editablegetset = new Editablegetset();
        editablegetset.setID(Integer.parseInt(cursor.getString(0)));
        editablegetset.setTitle(cursor.getString(1));
        editablegetset.setNote(cursor.getString(2));

        // Adding to list
        titlenoteslist.add(editablegetset);
    } while (cursor.moveToNext());
    }

    // return list
    cursor.close();
    db.close();
    return titlenoteslist;
} catch (Exception e) {
    // TODO: handle exception
    Log.e("all title and notes", "" + e);
}

return titlenoteslist;
}

// Updating individual notes
public int Update_Contact(Editablegetset editablegetset) {
SQLiteDatabase db = this.getWritableDatabase();

ContentValues values = new ContentValues();
values.put(KEY_TITLE, editablegetset.getTitle());
values.put(KEY_NOTE, editablegetset.getNote());


// updating row
return db.update(DATABASE_TABLE, values, KEY_ID + " = ?",
    new String[] { String.valueOf(editablegetset.getID()) });
}

// Deleting single notes
public void Delete_Contact(int id) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(DATABASE_TABLE, KEY_ID + " = ?",
    new String[] { String.valueOf(id) });
db.close();
}

// Getting notes Count
public int totalnotes() {
String countQuery = "SELECT  * FROM " + DATABASE_TABLE;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
cursor.close();

// return count
return cursor.getCount();
}

}

这是我的主要活动

package com.example.quicknotetaker;

import android.os.Bundle;
import android.app.Activity;

import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Mainnote extends Activity   {

DatabaseHandler db = new DatabaseHandler(this);

EditText edtitle, enotes;
Button ab;
int noteid;

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


    edtitle = (EditText) findViewById(R.id.title);
    enotes = (EditText)  findViewById(R.id.notes);

    ab = (Button)findViewById(R.id.addnote);

    ab.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Editablegetset ed = db.Get_Set(noteid);
            edtitle.setText(ed.getTitle());
            enotes.setText(ed.getNote());


            Toast.makeText(getApplicationContext(), 
                    "Note has been Added", Toast.LENGTH_LONG).show();

            Clear_Text();


        }

        public void Clear_Text() {
            // TODO Auto-generated method stub

            edtitle.getText().clear();
            enotes.getText().clear();


        }
    });
}
}

这是我的get set方法

package com.example.quicknotetaker;

public class Editablegetset {

// private variables
public int _id;
public String _title;
public String _note;


public Editablegetset() {
}

// constructor
public Editablegetset(int id, String title, String note) {
this._id = id;
this._title = title;
this._note = note;


}

// constructor
public Editablegetset(String title, String note) {
this._title = title;
this._note = note;

}

// getting ID
public int getID() {
return this._id;
}

// setting id
public void setID(int id) {
this._id = id;
}

// getting title
public String getTitle() {
return this._title;
}

// setting title
public void setTitle(String title) {
this._title = title;
}

// getting note
public String getNote() {
return this._note;
}

// setting note
public void setNote(String note) {
this._note = note;
}



}

日志猫

03-20 07:48:37.458: D/AndroidRuntime(23951): Shutting down VM
03-20 07:48:37.458: W/dalvikvm(23951): threadid=1: thread exiting with uncaught       exception (group=0x4170ad40)  
03-20 07:48:37.460: E/AndroidRuntime(23951): FATAL EXCEPTION: main 
03-20 07:48:37.460: E/AndroidRuntime(23951): Process: com.example.quicknotetaker, PID: 23951 
03-20 07:48:37.460: E/AndroidRuntime(23951): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.quicknotetaker/com.example.quicknotetaker.Mainnote}: java.lang.NullPointerException 
03-20 07:48:37.460: E/AndroidRuntime(23951):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209) 
03-20 07:48:37.460: E/AndroidRuntime(23951):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2269) 
03-20 07:48:37.460: E/AndroidRuntime(23951):    at android.app.ActivityThread.access$800(ActivityThread.java:139) 
03-20 07:48:37.460: E/AndroidRuntime(23951):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210) 
03-20 07:48:37.460: E/AndroidRuntime(23951):    at android.os.Handler.dispatchMessage(Handler.java:102) 
03-20 07:48:37.460: E/AndroidRuntime(23951):    at android.os.Looper.loop(Looper.java:136)
03-20 07:48:37.460: E/AndroidRuntime(23951):    at android.app.ActivityThread.main(ActivityThread.java:5102) 
03-20 07:48:37.460: E/AndroidRuntime(23951):    at java.lang.reflect.Method.invokeNative(Native Method) 
03-20 07:48:37.460: E/AndroidRuntime(23951):    at java.lang.reflect.Method.invoke(Method.java:515) 
03-20 07:48:37.460: E/AndroidRuntime(23951):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 
03-20 07:48:37.460: E/AndroidRuntime(23951):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
03-20 07:48:37.460: E/AndroidRuntime(23951):    at dalvik.system.NativeStart.main(Native Method) 
03-20 07:48:37.460: E/AndroidRuntime(23951): Caused by: java.lang.NullPointerException 
03-20 07:48:37.460: E/AndroidRuntime(23951):    at com.example.quicknotetaker.Mainnote.onCreate(Mainnote.java:30) 
03-20 07:48:37.460: E/AndroidRuntime(23951):    at android.app.Activity.performCreate(Activity.java:5248) 
03-20 07:48:37.460: E/AndroidRuntime(23951):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110) 
03-20 07:48:37.460: E/AndroidRuntime(23951):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2173) 
03-20 07:48:37.460: E/AndroidRuntime(23951):    ... 11 more

拜托我真的需要帮助我花了很多时间在这谢谢

【问题讨论】:

  • 例外是....?
  • 当我在我的安卓手机上运行它时,它只是说 - 不幸的是,快速笔记记录已停止。
  • 您需要从 logcat 发布堆栈跟踪。这将准确显示错误的位置。
  • 我现在已经添加了logcat
  • 由于onCreate() 中存在 NPE,因此布局可能没有 ID 为 addnote 的视图。

标签: java android eclipse sqlite


【解决方案1】:

“noteid”的值为 0,您可能没有值为 0 的 KEY_ID。db 查询可能不返回任何行。所以 Editablegetset 类设置为空。所以当你调用 edtitle.setText(ed.getTitle());你会得到 NullPointerException 因为 ed 是空的。在从 Editablegetset 类获取值之前添加一个空检查,如下代码所示

  if(ed!=null)
  {
  edtitle.setText(ed.getTitle());
  enotes.setText(ed.getNote());         
  }

同时正确设置“noteid”以匹配您的数据库值。

【讨论】:

    【解决方案2】:

    来自 logcat:

    Caused by: java.lang.NullPointerException
      at com.example.quicknotetaker.Mainnote.onCreate(Mainnote.java:30)
    

    你的onCreate()真的只能在这里NPE:

    ab.setOnClickListener(...);
    

    ab 为空时。你用findViewById() 初始化它,如果没有找到视图,它会返回null

    确保您的activity_mainnote 布局确实有一个Button,ID 为addnote

    【讨论】:

    • 您的权利。布局有问题。对不起,我已经很久了。你能看到添加按钮的任何错误吗,当我按下它时应用程序崩溃了。
    • 在 logcat 中也使用异常堆栈跟踪开始调试那个。
    • 它会说 03-20 08:19:46.698: E/OpenGLRenderer(26027): GL_INVALID_OPERATION
    • 你认为如果我添加自动增量它会解决问题。
    【解决方案3】:

    基于我在您的代码中的观点。您在 main 中的 int 变量 noteid 中获得空指针。

    尝试使用这个

    int noteid = 0;
    

    希望这会对你有所帮助。

    【讨论】:

    • 你确定吗?但根据我的经验。如果我遇到空指针异常,我总是初始化我的变量。
    • 我无法检查您的指向,但对我来说。如果你初始化你的变量会更好:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-22
    • 2018-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多