Reputation: 919
The goal of this mini project is if input the message, linking to the result xml and show the result. But after AVD running and click [add] button, app stopped... How to fix this problem? I want to know the responsibility of this problem (just code, AVD ... )
Here is java file and codes.
[DBHelper.java]
package com.example.pr_0813;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DBHelper extends SQLiteOpenHelper {
public static final int DATABASE_VERSION = 1;
public DBHelper(Context context) {
super(context, "memodb", null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String memoSQL = "create table tb_memo " +
"(_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"content)";
db.execSQL(memoSQL);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS tb_memo");
onCreate(db);
}
}
[MainActivity.java]
package com.example.pr_0813;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
EditText input_msg;
Button add_btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
input_msg = findViewById(R.id.add_content);
add_btn = findViewById(R.id.add_btn);
add_btn.setOnClickListener(this);
}
public void onClick(View v) {
String content = input_msg.getText().toString();
DBHelper helper = new DBHelper(this);
SQLiteDatabase db = helper.getWritableDatabase();
db.execSQL("insert into tb_memo (_id, content) values (?,?)");
db.close();
Intent intent = new Intent(this, ReadDBActivity.class);
startActivity(intent);
}
}
[ReadDActivity.java]
package com.example.pr_0813;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class ReadDBActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_read_db);
TextView content_view = findViewById(R.id.read_content);
DBHelper helper = new DBHelper(this);
SQLiteDatabase db = helper.getWritableDatabase();
Cursor cursor = db.rawQuery("select content from tb_memo order by _id desc limit 1", null);
while(cursor.moveToNext()) {
content_view.setText(cursor.getString(0));
}
db.close();
}
}
xml views [activity_main.xml]
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:background="#696969"/>
<EditText
android:id="@+id/add_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="INPUT YOUR MESSAGE"
android:textSize="30dp"
android:textStyle="bold"/>
<Button
android:id="@+id/add_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ADD"
android:textStyle="bold"
android:textSize="30dp"/>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:background="#696969"/>
[activity_read_db.xml]
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="=== R E S U L T ==="
android:textSize="30dp"/>
<TextView
android:id="@+id/read_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30dp"
android:textStyle="bold"/>
Upvotes: 0
Views: 615
Reputation: 1675
First, you can find out the stack trace in logcat if it crashes. Then you know where it crashes.
Without the stack trace, we can only guess the problem. Possible reason may be 1. create table sql has a problem.
String memoSQL = "create table tb_memo " +
"(_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"content)";
content has no type. you can change to
String memoSQL = "create table tb_memo " +
"(_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"content TEXT )";
second, sql to insert table has no values. you can use ContentValues to avoid writing raw sql.
SQLiteDatabase db = helper.getWritableDatabase();
db.execSQL("insert into tb_memo (_id, content) values (?,?)");
db.close();
change to
SQLiteDatabase db = helper.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put("content", add_content.getText().toString());
db.insert("tb_memo", cv, null);
_id is not needed as it is auto increment.
Upvotes: 3