我是Android编程的新手,想要一些帮助或建议为什么每当我尝试通过点击更新按钮更新数据库中的记录时,要传递的id为null,因此它不会更新。如何获取它以便在更新时Id不为空。
DatabaseHelper.java
package com.example.warre.sqlliteapp;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "tracker.db";
public static final String TABLE_NAME = "tracker_table";
public static final String COL_1 = "ID";
public static final String COL_2 = "SALES";
public static final String COL_3 = "FOLLOW";
public static final String COL_4 = "DONE";
public static final String COL_5 = "LEADER";
public static final String COL_6 = "TRAIN";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + TABLE_NAME +" (ID INTEGER PRIMARY KEY AUTOINCREMENT,SALES INTEGER,FOLLOW INTEGER,DONE INTEGER, LEADER INTEGER, TRAIN INTEGER)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
onCreate(db);
}
public boolean insertData(String sales,String follow,String done, String leader, String train) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_2,sales);
contentValues.put(COL_3,follow);
contentValues.put(COL_4,done);
contentValues.put(COL_5,leader);
contentValues.put(COL_6, train);
long result = db.insert(TABLE_NAME,null ,contentValues);
if(result == -1)
return false;
else
return true;
}
public Cursor getAllData() {
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("select * from "+TABLE_NAME,null);
return res;
}
public boolean updateData(String id,String sales,String follow,String done, String leader, String train) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_1,id);
contentValues.put(COL_2,sales);
contentValues.put(COL_3,follow);
contentValues.put(COL_4,done);
contentValues.put(COL_5, leader);
contentValues.put(COL_6, train);
db.update(TABLE_NAME, contentValues, "ID = ?",new String[] { id });
return true;
}
public Integer deleteData (String id) {
SQLiteDatabase db = this.getWritableDatabase();
return db.delete(TABLE_NAME, "ID = ?",new String[] {id});
}
}
MainActivity
问题发生在updateData()中,其中editTextId变为null或返回null。
package com.example.warre.sqlliteapp;
import android.app.Activity;
import android.database.Cursor;
import android.os.Handler;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends Activity {
DatabaseHelper myDb;
EditText editTextId;
TextView txtSales,txtFollow,txtDone,txtLeader,txtTrain, TimerValue;
Button btnAddData;
Button btnviewAll;
Button btnDelete;
Button SALES,FOLLOW,DONE,LEADER,TRAIN, STOP, REPORT;
int countSales = 0;
int countFollow = 0;
int countDone = 0;
int nCounter = 0;
String timeName;
TimerTask mTimerTask;
final Handler handler = new Handler();
Timer t = new Timer();
Button btnviewUpdate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_money);
myDb = new DatabaseHelper(this);
TimerValue = (TextView) findViewById(R.id.tv_current_timeV2);
txtSales = (TextView) findViewById(R.id.textSales);
txtFollow = (TextView) findViewById(R.id.textFollow);
txtDone = (TextView) findViewById(R.id.textDone);
txtLeader = (TextView) findViewById(R.id.leaderTime);
txtTrain = (TextView) findViewById(R.id.trainTime);
SALES = (Button) findViewById(R.id.btnSales);
FOLLOW = (Button) findViewById(R.id.btnFollow);
DONE = (Button) findViewById(R.id.btnDone);
LEADER = (Button) findViewById(R.id.btnLeader);
TRAIN = (Button) findViewById(R.id.btnTrain);
STOP = (Button) findViewById(R.id.btnStop);
REPORT = (Button) findViewById(R.id.button_details);
editTextId = (EditText)findViewById(R.id.editText_id);
btnAddData = (Button)findViewById(R.id.button_add);
btnviewAll = (Button)findViewById(R.id.button_viewAll);
btnviewUpdate= (Button)findViewById(R.id.button_update);
btnDelete= (Button)findViewById(R.id.button_delete);
TimerValue.setText("00:00:00");
txtSales.setText("0");
txtFollow.setText("0");
txtDone.setText("0");
txtLeader.setText("00:00:00");
txtTrain.setText("00:00:00");
viewAll();
AddData();
UpdateData();
CountSales();
CountFollow();
CountDone();
TimeLeader();
TimeTrain();
StopTime();
}
public void CountSales() {
SALES.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
countSales++;
txtSales.setText(String.valueOf(countSales));
}
}
);
}
public void CountFollow() {
FOLLOW.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
countFollow++;
txtFollow.setText(String.valueOf(countFollow));
}
}
);
}
public void CountDone() {
DONE.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
countDone++;
txtDone.setText(String.valueOf(countDone));
}
}
);
}
public void TimeLeader() {
LEADER.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
SALES.setClickable(false);
FOLLOW.setClickable(false);
DONE.setClickable(false);
LEADER.setClickable(false);
TRAIN.setClickable(false);
timeName = "leader";
mTimerTask = new TimerTask() {
public void run() {
handler.post(new Runnable() {
public void run() {
nCounter++;
TimerValue.setText("" + Utils.secToString(Integer.parseInt(String.valueOf(nCounter))));
txtLeader.setText("" + Utils.secToString(Integer.parseInt(String.valueOf(nCounter))));
}
});
}};
t.schedule(mTimerTask, 200, 1000); //
}
}
);
}
public void TimeTrain() {
TRAIN.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
SALES.setClickable(false);
FOLLOW.setClickable(false);
DONE.setClickable(false);
LEADER.setClickable(false);
TRAIN.setClickable(false);
timeName = "train";
mTimerTask = new TimerTask() {
public void run() {
handler.post(new Runnable() {
public void run() {
nCounter++;
TimerValue.setText("" + Utils.secToString(Integer.parseInt(String.valueOf(nCounter))));
txtTrain.setText("" + Utils.secToString(Integer.parseInt(String.valueOf(nCounter))));
}
});
}};
t.schedule(mTimerTask, 200, 1000); //
}
}
);
}
public void StopTime() {
STOP.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
SALES.setClickable(true);
FOLLOW.setClickable(true);
DONE.setClickable(true);
LEADER.setClickable(true);
TRAIN.setClickable(true);
if (mTimerTask != null) {
TimerValue.setText("" + Utils.secToString(Integer.parseInt(String.valueOf(nCounter))));
mTimerTask.cancel();
if (timeName == "leader") {
TimerValue.setText(Utils.secToString(0));
txtLeader.setText("" + Utils.secToString(Integer.parseInt(String.valueOf(nCounter))));
} else if (timeName == "train") {
TimerValue.setText(Utils.secToString(0));
txtTrain.setText("" + Utils.secToString(Integer.parseInt(String.valueOf(nCounter))));
}
}
}
}
);
}
public void AddData() {
btnAddData.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean isInserted = myDb.insertData(txtSales.getText().toString(),
txtFollow.getText().toString(),
txtDone.getText().toString(), txtLeader.getText().toString(), txtTrain.getText().toString() );
if(isInserted == true) {
Toast.makeText(MainActivity.this, "Data Inserted", Toast.LENGTH_LONG).show();
btnAddData.setVisibility(v.GONE);
}
else
Toast.makeText(MainActivity.this,"Data not Inserted",Toast.LENGTH_LONG).show();
}
}
);
}
public void UpdateData() {
btnviewUpdate.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean isUpdate = myDb.updateData(editTextId.getText().toString(),
txtSales.getText().toString(),
txtFollow.getText().toString(),
txtDone.getText().toString(), txtLeader.getText().toString(), txtTrain.getText().toString() );
if(isUpdate == true)
Toast.makeText(MainActivity.this,"Data Update",Toast.LENGTH_LONG).show();
else
Toast.makeText(MainActivity.this,"Data not Updated",Toast.LENGTH_LONG).show();
}
}
);
}
public void viewAll() {
REPORT.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
Cursor res = myDb.getAllData();
if(res.getCount() == 0) {
// show message
showMessage("Error","Nothing found");
return;
}
StringBuffer buffer = new StringBuffer();
while (res.moveToNext()) {
buffer.append("Id :"+ res.getString(0)+"\n");
buffer.append("Sales :"+ res.getString(1)+"\n");
buffer.append("Follow :"+ res.getString(2)+"\n");
buffer.append("Done :"+ res.getString(3)+"\n");
buffer.append("Leader :"+ res.getString(4)+"\n");
buffer.append("Train :"+ res.getString(5)+"\n" );
}
// Show all data
showMessage("Data",buffer.toString());
}
}
);
}
public void showMessage(String title,String Message){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(Message);
builder.show();
}
}
activity_money
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:id="@+id/tv_current_timeV2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="00:00:00"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@android:color/holo_green_light"
android:textSize="38dp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:id="@+id/btnSales"
android:layout_width="168dp"
android:layout_height="match_parent"
android:layout_marginLeft="100dp"
android:onClick="onClick"
android:text="SALES" />
<TextView
android:id="@+id/textSales"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="0"
android:textStyle="bold"
android:textColor="@color/colorWhite"
android:gravity="center"/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:id="@+id/btnFollow"
android:layout_width="168dp"
android:layout_height="match_parent"
android:layout_marginLeft="100dp"
android:onClick="onClick"
android:text="FOLLOW"
/>
<TextView
android:id="@+id/textFollow"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="0"
android:textStyle="bold"
android:textColor="@color/colorWhite"
android:gravity="center"/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:id="@+id/btnDone"
android:layout_width="168dp"
android:layout_height="match_parent"
android:onClick="onClick"
android:layout_marginLeft="100dp"
android:text="DONE" />
<TextView
android:id="@+id/textDone"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="0"
android:textStyle="bold"
android:textColor="@color/colorWhite"
android:gravity="center"/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:id="@+id/btnLeader"
android:layout_width="168dp"
android:layout_height="match_parent"
android:layout_marginLeft="100dp"
android:onClick="onClick"
android:text="LEADER" />
<TextView
android:id="@+id/leaderTime"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="00:00:00"
android:textStyle="bold"
android:textColor="@color/colorWhite"
android:gravity="center"/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:id="@+id/btnTrain"
android:layout_width="168dp"
android:layout_height="match_parent"
android:onClick="onClick"
android:layout_marginLeft="100dp"
android:text="TRAIN" />
<TextView
android:id="@+id/trainTime"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="00:00:00"
android:textStyle="bold"
android:textColor="@color/colorWhite"
android:gravity="center"/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent" />
<Button
android:id="@+id/btnStop"
android:layout_width="168dp"
android:layout_height="match_parent"
android:layout_marginLeft="100dp"
android:onClick="onClick"
android:text="STOP" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent" />
<Button
android:id="@+id/button_details"
android:layout_width="168dp"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:text="REPORT"
android:textColor="@color/colorBlack"
android:onClick="onClick"
android:textStyle="bold"/>
<Button
android:id="@+id/button_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/editText_Marks"
android:layout_marginTop="76dp"
android:text="Add Data" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update"
android:id="@+id/button_update"
android:layout_below="@+id/button_add"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</LinearLayout>
</ScrollView>
错误记录
答案 0 :(得分:0)
好的,所以你的错误原因是因为这行
editTextId = (EditText)findViewById(R.id.editText_id);
因为EditText
布局中没有activity_money.xml
,并且您正在尝试获取其引用,因此您将获得空引用。
首先将其添加到xml
或从java文件中删除edittext
行。