我的应用程序崩溃需要帮助才能修复它。
MainActivity.java
public class MainActivity extends AppCompatActivity {
DatabaseHelper myDB;
Button btnAdd;
Button btnList;
TextView tvView;
EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnAdd = (Button) findViewById(R.id.btnAdd);
btnList = (Button) findViewById(R.id.btnList);
tvView = (TextView) findViewById(R.id.Textview);
editText = (EditText) findViewById(R.id.editText);
myDB=new DatabaseHelper(this);
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String newEntry = editText.getText().toString();
if (newEntry.length() != 0) {
AddData(newEntry);
editText.setText("");
} else {
Toast.makeText(MainActivity.this, "You must put something in the text field", Toast.LENGTH_LONG).show();
}
}
});
btnList.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, ListDataActivity.class);
startActivity(intent);
}
});
}
public void AddData(String newEntry) {
boolean insertData = myDB.addData(newEntry);
// check inserted successfully
if (insertData == true) {
Toast.makeText(MainActivity.this, "Successfully Entered Data!", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(MainActivity.this, "Something went wrong", Toast.LENGTH_LONG).show();
}
}
}
ListDataActivity.java
public class ListDataActivity extends AppCompatActivity {
DatabaseHelper myDB;
ListView listView;
ArrayAdapter<String>listAdapter;
@Override
protected void onCreate( Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.list_layout);
listView=(ListView)findViewById(R.id.listView);
myDB= new DatabaseHelper(this);
//populate an ArrayList<String> from the databases and then view it
ArrayList<String> theList=new ArrayList<>();
Cursor data=myDB.getListContent();
if(data.getCount()==0){
Toast.makeText(ListDataActivity.this,"The database was empty",Toast.LENGTH_LONG).show();
}else{
while(data.moveToNext()){
// get the value from the database in column 1
// then add it to the ArrayList
theList.add(data.getString(1));
// create the list adapter and set the adapter
listAdapter=new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,theList);
listView.setAdapter(listAdapter);
// set an onItemClickListen to the listView
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String name=adapterView.getItemAtPosition(i).toString();
Cursor data=myDB.getItemID(name); //get the id assosicated with the name
int itemID=1;
while(data.moveToNext()){
itemID=data.getInt(0);
}
if(itemID > -1){
Intent editScreenIntent=new Intent(ListDataActivity.this,EditDataActivity.class);
editScreenIntent.putExtra("id",itemID);
editScreenIntent.putExtra("name",name);
startActivity(editScreenIntent);
}
else{
Toast.makeText(ListDataActivity.this, "No ID assosciated", Toast.LENGTH_SHORT).show();
}
}
});
}
}
}
}
EditDataActivity.java
public class EditDataActivity extends AppCompatActivity {
Button btnDelete;
EditText etDelete;
DatabaseHelper myDB;
String selectedName;
int selectedID;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.edit_data_layout);
btnDelete=(Button)findViewById(R.id.btnDelete);
etDelete=(EditText)findViewById(R.id.editText);
myDB=new DatabaseHelper(this);
// get the intent extra from the ListDataActivity
Intent receivedIntent=getIntent();
// now get the itemID we passed as an extra
selectedID=receivedIntent.getIntExtra("id",-1);//NOTE -1 it just a default values
// now get the name we passed as an extra
selectedName=receivedIntent.getStringExtra("name");
//set the text to show the current selected name
etDelete.setText(selectedName);
btnDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
myDB.deleteName(selectedID,selectedName);
etDelete.setText("");
Toast.makeText(EditDataActivity.this, "removed from datebase", Toast.LENGTH_SHORT).show();
}
});
}
}
DatabaseHelper.java
公共类DatabaseHelper扩展了SQLiteOpenHelper {
public static final String DATABASE_NAME = "mylist.db";
public static final String TABLE_NAME = "mylist_data";
public static final String COL1 = "ID";
public static final String COL2 = "ITEM1";
public DatabaseHelper(Context context){
super (context, DATABASE_NAME, null , 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
String createTable = " CREATE TABLE " + TABLE_NAME +"(ID INTEGER PRIMARY KEY AUTOINCREMENT," +
" ITEM1 TEXT)";
db.execSQL(createTable);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP IF TABLE EXITS "+ TABLE_NAME);
onCreate(db);
}
public boolean addData(String item1) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL2, item1);
long result = db.insert(TABLE_NAME, null, contentValues);
//if date as instered incorrectly it will return -1
if (result == -1) {
return false;
} else {
return true;
}
}
/**
* Return all the data from database
* @return
*/
public Cursor getListContent() {
SQLiteDatabase db = this.getWritableDatabase();
Cursor data =db.rawQuery("SELECT * FROM " + TABLE_NAME,null);
return data;
}
/**
* Returns only the ID that matches the name passed in
* @param name
* @return
*/
public Cursor getItemID(String name){
SQLiteDatabase db=this.getWritableDatabase();
String query = "SELECT "+ COL1 +" FROM "+TABLE_NAME+
" WHERE "+ COL2 +" = '"+ name +"'";
Cursor data = db.rawQuery(query,null);
return data;
}
/**
* Delete from database
* @param id
* @param name
*/
public void deleteName(int id, String name){
SQLiteDatabase db=this.getWritableDatabase();
String query= "DELETE FROM "+ TABLE_NAME +" WHERE " + COL1 + " = '"
+ id + "'"+" AND " + COL2 + "= '" + name + "'";
db.execSQL(query);
}
}
布局
<EditText
android:id="@+id/etDelete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" />
<Button android:id="@+id/btnDelete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Delete" />
LogCat Show:
12-28 02:32:39.527 15020-15020/sg.edu.rp.c346.todolist E/dalvikvm: Could not find class 'android.graphics.drawable.RippleDrawable', referenced from method android.support.v7.widget.AppCompatImageHelper.hasOverlappingRendering
12-28 02:34:22.487 15020-15020/sg.edu.rp.c346.todolist E/AndroidRuntime: FATAL EXCEPTION: main
Process: sg.edu.rp.c346.todolist, PID: 15020
java.lang.RuntimeException: Unable to start activity ComponentInfo{sg.edu.rp.c346.todolist/sg.edu.rp.c346.todolist.EditDataActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
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:5017)
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:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at sg.edu.rp.c346.todolist.EditDataActivity.onCreate(EditDataActivity.java:42)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
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:5017)
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:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
问题在这里:
etDelete = (EditText) findViewById(R.id.editText);
更改为:
etDelete = (EditText) findViewById(R.id.etDelete);
//-----------------------------------------^