我想更新数据库中的数据,但它在 sqldb.update 行中给出 java.lang.NullPointerException ...我已经搜索了如何更新数据但它仍然相同,请告诉我错误
这里有一些 content.java
package ta.ens;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.ClipboardManager;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;
public class Content extends Activity {
/** Called when the activity is first created. */
//TextView judul,hadits,arti;
ListView content;
SimpleCursorAdapter adapter;
MyDB mydb;
Cursor cursor,populer;
TextView judul,hadits,arti;
SQLiteDatabase sqldb;
Context context=this;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_list_hadits);
mydb = new MyDB(this);
judul = (TextView)findViewById(R.id.textjudul);
hadits = (TextView)findViewById(R.id.texthadits);
arti = (TextView)findViewById(R.id.textarti);
Bundle extras = getIntent().getExtras();
String id = extras.getString("row");
String namajudul = extras.getString("rowjudul");
ContentValues cv = new ContentValues();
populer = mydb.getPopularity(id);
int jumlah = populer.getCount();
if (jumlah == 0){
cv.put("totalquery", 1);
sqldb.update("populer", cv, "_id ?", new String[]{id});
}else {
jumlah = jumlah + 1;
cv.put("totalquery", jumlah);
sqldb.rawQuery("UPDATE populer SET totalquery = "+jumlah+" WHERE _id =" +id, null);
}
judul.setText(namajudul);
hadits.setTypeface(Typeface.createFromAsset(getAssets(), "dejavusans.ttf"));
cursor = mydb.getHadits(id);
int total = cursor.getCount();
for(int i = 1;i<=total;i++){
String isihadits = ArabicUtilities.reshape(cursor.getString(cursor.getColumnIndex("hadits")));
String isiarti = cursor.getString(cursor.getColumnIndex("arti"));
hadits.setText(isihadits);
arti.setText(isiarti);
cursor.moveToNext();
}
hadits.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
ClipboardManager clipboard = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);
clipboard.setText(hadits.getText());
Toast.makeText(context, "Copied to clipboard", Toast.LENGTH_SHORT).show();
}
});
arti.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
ClipboardManager clipboard = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);
clipboard.setText(arti.getText());
Toast.makeText(context, "Copied to clipboard", Toast.LENGTH_SHORT).show();
}
});
}
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menuhelp, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item){
Intent intent = new Intent();
switch(item.getItemId()){
case R.id.helptema :
finish();
intent.setClass(Content.this, Tema.class);
startActivity(intent);
break;
case R.id.searchhelp :
finish();
intent.setClass(Content.this, Cari.class);
startActivity(intent);
break;
case R.id.tentanghelp :
finish();
intent.setClass(Content.this, Tentang.class);
startActivity(intent);
break;
case R.id.quithelp :
finish();
System.exit(0);
}
return true;
}
}
这是 mydb.java
package ta.ens;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.readystatesoftware.sqliteasset.SQLiteAssetHelper;
public class MyDB extends SQLiteAssetHelper{
private static final String DATABASE_NAME = "hadits";
private static final int DATABASE_VERSION = 1;
public MyDB(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
public Cursor getAllTema(){
SQLiteDatabase sqldb = getReadableDatabase();
Cursor c = sqldb.rawQuery("SELECT * FROM tema", null);
c.moveToFirst();
return c;
}
public Cursor getJudul(String id){
SQLiteDatabase sqldb = getReadableDatabase();
Cursor jdl = sqldb.rawQuery("SELECT judul._id,judul.judul FROM judul,tema,temajudul WHERE tema._id = temajudul.idtema AND judul._id = temajudul.idjudul AND tema._id = "+id, null);
jdl.moveToFirst();
return jdl;
}
public Cursor getHadits(String id){
SQLiteDatabase sqldb = getReadableDatabase();
Cursor hadits = sqldb.rawQuery("SELECT hadits.hadits , hadits.arti FROM hadits,judul,judulhadits WHERE judul._id = judulhadits.idjudul AND hadits.rowid = judulhadits.idhadits AND judul._id = "+id, null);
hadits.moveToFirst();
return hadits;
}
public void virtualtabel(){
SQLiteDatabase sqldb = getReadableDatabase();
sqldb.execSQL("INSERT INTO virtualhadits SELECT * FROM hadits");
}
public Cursor getAllHadits(){
SQLiteDatabase sqldb = getReadableDatabase();
Cursor allcontent = sqldb.rawQuery("SELECT * FROM virtualhadits", null);
return allcontent;
}
public Cursor getCari(String key){
SQLiteDatabase sqldb = getReadableDatabase();
Cursor cari = sqldb.rawQuery("SELECT virtualhadits.rowid AS _id , judul.judul FROM judul,judulhadits,virtualhadits WHERE judul._id = judulhadits.idjudul AND virtualhadits.rowid = judulhadits.idhadits AND virtualhadits MATCH '"+key+"'",
null);
cari.moveToFirst();
return cari;
}
public Cursor getPopularity(String id){
SQLiteDatabase sqldb = getReadableDatabase();
Cursor semua = sqldb.rawQuery("SELECT totalquery FROM populer WHERE _id = "+id, null);
return semua;
}
public Cursor getPopularitasJudul(){
SQLiteDatabase sqldb = getReadableDatabase();
Cursor populerjudul = sqldb.rawQuery("SELECT judul._id judul.judul FROM judul,hadits,populer WHERE judul._id = judulhadits.idjudul AND hadits._id = judulhadits.idhadits AND populer._id = hadits._id ORDER BY populer.totalquery DESC", null);
return populerjudul;
}
}
这里日志猫
12-14 13:28:02.821: ERROR/AndroidRuntime(5867): FATAL EXCEPTION: main
12-14 13:28:02.821: ERROR/AndroidRuntime(5867): java.lang.RuntimeException: Unable to start activity ComponentInfo{ta.ens/ta.ens.Content}: java.lang.NullPointerException
12-14 13:28:02.821: ERROR/AndroidRuntime(5867): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
12-14 13:28:02.821: ERROR/AndroidRuntime(5867): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
12-14 13:28:02.821: ERROR/AndroidRuntime(5867): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
12-14 13:28:02.821: ERROR/AndroidRuntime(5867): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
12-14 13:28:02.821: ERROR/AndroidRuntime(5867): at android.os.Handler.dispatchMessage(Handler.java:99)
12-14 13:28:02.821: ERROR/AndroidRuntime(5867): at android.os.Looper.loop(Looper.java:123)
12-14 13:28:02.821: ERROR/AndroidRuntime(5867): at android.app.ActivityThread.main(ActivityThread.java:4627)
12-14 13:28:02.821: ERROR/AndroidRuntime(5867): at java.lang.reflect.Method.invokeNative(Native Method)
12-14 13:28:02.821: ERROR/AndroidRuntime(5867): at java.lang.reflect.Method.invoke(Method.java:521)
12-14 13:28:02.821: ERROR/AndroidRuntime(5867): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-14 13:28:02.821: ERROR/AndroidRuntime(5867): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-14 13:28:02.821: ERROR/AndroidRuntime(5867): at dalvik.system.NativeStart.main(Native Method)
12-14 13:28:02.821: ERROR/AndroidRuntime(5867): Caused by: java.lang.NullPointerException
12-14 13:28:02.821: ERROR/AndroidRuntime(5867): at ta.ens.Content.onCreate(Content.java:54)
12-14 13:28:02.821: ERROR/AndroidRuntime(5867): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-14 13:28:02.821: ERROR/AndroidRuntime(5867): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
12-14 13:28:02.821: ERROR/AndroidRuntime(5867): ... 11 more