这是MainAcitivity的源代码
package dhyna.inventory;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
public PengolahDatabase mydb;
private ListView listData = null;
private SQLiteDatabase db = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mydb = new PengolahDatabase(this);
db = mydb.getWritableDatabase();
setContentView(R.layout.activity_main);
tampilSemuaProduk(db);
listItemClickListener();
mydb.close();
}
private void tampilSemuaProduk(SQLiteDatabase db) {
Cursor tampilCursor;
tampilCursor = mydb.fetchAllWisata(db);
startManagingCursor(tampilCursor);
String[] from = new String[]{mydb.ISI_TB_PRODUK[0], mydb.ISI_TB_PRODUK[1], mydb.ISI_TB_PRODUK[2], mydb.ISI_TB_PRODUK[3],mydb.ISI_TB_PRODUK[4]};
int[] to = new int[] {R.id.tv_id, R.id.tv_produk, R.id.tv_kat, R.id.tv_hargabeli, R.id.tv_hargajual};
SimpleCursorAdapter tampilAdapter = new SimpleCursorAdapter(this, R.layout.row, tampilCursor, from, to);
listData = (ListView) findViewById(R.id.listView);
listData.setAdapter(tampilAdapter);
}
public void onDestroy() {
super.onDestroy();
try {
db.close();
} catch (Exception e){}
}
public void listItemClickListener(){
listData.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
kirimParamIntent(position);
}
});
}
public void kirimParamIntent(int posisi) {
Intent intent = new Intent(this, TampilDetilProduk.class);
intent.putExtra("idProduk", posisi++);
startActivity(intent);
}
}
这是我的DBhelper类,我将它命名为PengolahDataBase.java
package dhyna.inventory;
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 PengolahDatabase extends SQLiteOpenHelper {
private static final String NAMA_DB = "inventori";
private final String TB_PRODUK = "produk";
// private final String TB_KATEGORI = "kategori";
public final String[] ISI_TB_PRODUK = {"_id", "nama_produk", "id_kat", "harga_beli", "harga_jual"};
// public final String[] ISI_TB_KATEGORI = {"_id", "nama_kategori"};
PengolahDatabase (Context context){
super(context, NAMA_DB, null, 1);
}
public void createTable(SQLiteDatabase db){
db.execSQL("DROP TABLE IF EXISTS "+TB_PRODUK+"");
db.execSQL("CREATE TABLE if not exists " + TB_PRODUK + "(" + ISI_TB_PRODUK[0] + " INTEGER PRIMARY KEY AUTOINCREMENT, " + ISI_TB_PRODUK[1] + " TEXT, " + ISI_TB_PRODUK[2] + " INTEGER, " + ISI_TB_PRODUK[3] + " INTEGER, " + ISI_TB_PRODUK[4] + " INTEGER)");
}
public void dataStandar (SQLiteDatabase db) {
String[] produk = new String[]{"Marlboro", "Beras Tabanan", "Kopi Bola Dunia"};
String[] kat = new String[]{"Rokok", "Sembako", "Sembako"};
int[] hargaBeli = new int[]{19000, 199000, 20000};
int[] hargaJual = new int[]{23000, 210000, 23000};
ContentValues cv = new ContentValues();
int i;
int batas = produk.length - 1;
for (i = 0; i <= batas; i++) {
cv.put(ISI_TB_PRODUK[1], produk[i]);
cv.put(ISI_TB_PRODUK[2], kat[i]);
cv.put(ISI_TB_PRODUK[3], hargaBeli[i]);
cv.put(ISI_TB_PRODUK[4], hargaJual[i]);
db.insert(TB_PRODUK, null, cv);
cv.clear();
}
}
public Cursor fetchAllWisata(SQLiteDatabase db){
return db.query(TB_PRODUK, new String[] {ISI_TB_PRODUK[0], ISI_TB_PRODUK[1], ISI_TB_PRODUK[2], ISI_TB_PRODUK[3], ISI_TB_PRODUK[4]},null , null, null, null, null);
}
public Barang ambilDetilProduk(SQLiteDatabase db,long idProduk){
Log.v("info", "var db "+db);
Cursor cursor = db.query(TB_PRODUK, new String[]{ISI_TB_PRODUK[0],ISI_TB_PRODUK[1]}, ISI_TB_PRODUK[0] + " = " + idProduk, null, null, null, null);
cursor.moveToFirst();
Barang barang = cursorToBarang(cursor);
cursor.close();
return barang;
}
private Barang cursorToBarang(Cursor cursor) {
Barang barang = new Barang();
Log.v("info", "The GetLong " + cursor.getLong(0));
barang.setIdProduk(cursor.getLong(0));
barang.setNamaProduk(cursor.getString(1));
return barang;
}
@Override
public void onCreate(SQLiteDatabase db) {
createTable(db);
dataStandar(db);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
在MainAcitivity中,我创建了listView,用于显示数据库中的数据。这个过程很顺利。我也做了一个事件来处理listView上的itemClick。如果我单击listView中的一行,它将发送一个包含id的var并将其转换为具有intent的第二个acitivity。在第二个Activity中,它将显示DataBase中与intent相同的数据,并使用TextView显示它。
我有两个版本的Second Activty代码(我给它命名为TampilDetilProduk.java)。第一个版本代码运行顺利,没有错误。但在第二个版本中存在Java.nullPointerException问题,因此它将强制关闭。我的问题是两者有什么不同?
这是一个从DB保存信息的界面 包dhyna.inventory;
/**
* Created by Gung Bagus on 1/19/2016.
*/
public class Barang {
private long idProduk ;
private String namaProduk="Produk Tidak Ditemukan";
private int hargaBeli, hargaJual;
public Barang(){}
public void setIdProduk(long id){
this.idProduk = id;
}
public void setNamaProduk(String produk){
this.namaProduk = produk;
}
public void setHargaBeli(int harga){
this.hargaBeli = harga;
}
public void setHargaJual (int harga){
this.hargaJual = harga;
}
public long getIdProduk(){
return this.idProduk;
}
public String getNamaProduk() {
return this.namaProduk;
}
public int getHargaBeli(){
return this.hargaBeli;
}
public int getHargaJual(){
return this.hargaJual;}
}
这里是第一个版本的第二个活动
package dhyna.inventory;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
public class TampilDetilProduk extends AppCompatActivity {
TextView namaProduk, namaKat, hargaBeli, hargaJual;
ImageView tombolKembali;
PengolahDatabase mydb;
SQLiteDatabase db = null;
Barang barang= null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
PengolahDatabase mydb = new PengolahDatabase(this);
db = mydb.getWritableDatabase();
Intent intent = getIntent();
long idProduk = intent.getIntExtra("idProduk", 0) + 1;
//Log.v("Info", "Nilai id Intent " + idProduk);
barang = mydb.ambilDetilProduk(db, idProduk);
setContentView(R.layout.activity_tampil_detil_produk);
namaProduk = (TextView) findViewById(R.id.tv_produk_detil);
namaProduk.setText(barang.getNamaProduk());
}
private void tampilkanProduk(SQLiteDatabase dbl) {
namaProduk = (TextView) findViewById(R.id.tv_produk_detil);
Intent intent = getIntent();
long idProduk = intent.getIntExtra("idProduk", 0) + 1;
Log.v("Info", "Nilaiid Intent"+idProduk);
barang = mydb.ambilDetilProduk(dbl, idProduk);
namaProduk.setText(barang.getNamaProduk());
}
}
这里是第二版
package dhyna.inventory;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
public class TampilDetilProduk extends AppCompatActivity {
TextView namaProduk, namaKat, hargaBeli, hargaJual;
ImageView tombolKembali;
PengolahDatabase mydb;
SQLiteDatabase db = null;
Barang barang= null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
PengolahDatabase mydb = new PengolahDatabase(this);
db = mydb.getWritableDatabase();
setContentView(R.layout.activity_tampil_detil_produk);
tampilkanProduk(db);
}
private void tampilkanProduk(SQLiteDatabase dbl) {
namaProduk = (TextView) findViewById(R.id.tv_produk_detil);
Intent intent = getIntent();
long idProduk = intent.getIntExtra("idProduk", 0) + 1;
Log.v("Info", "Nilai id Intent"+idProduk);
barang = mydb.ambilDetilProduk(dbl, idProduk);
namaProduk.setText(barang.getNamaProduk());
}
}
这是logCat
01-16 23:02:37.111 11917-11917/dhyna.inventory I/dalvikvm: Could not find method android.widget.LinearLayout$LayoutParams.<init>, referenced from method android.support.design.widget.AppBarLayout$LayoutParams.<init>
01-16 23:02:37.111 11917-11917/dhyna.inventory W/dalvikvm: VFY: unable to resolve direct method 15952: Landroid/widget/LinearLayout$LayoutParams;.<init> (Landroid/widget/LinearLayout$LayoutParams;)V
01-16 23:02:37.111 11917-11917/dhyna.inventory D/dalvikvm: VFY: replacing opcode 0x70 at 0x0000
01-16 23:02:37.121 11917-11917/dhyna.inventory I/dalvikvm: Could not find method android.widget.LinearLayout$LayoutParams.<init>, referenced from method android.support.design.widget.AppBarLayout$LayoutParams.<init>
01-16 23:02:37.121 11917-11917/dhyna.inventory W/dalvikvm: VFY: unable to resolve direct method 15952: Landroid/widget/LinearLayout$LayoutParams;.<init> (Landroid/widget/LinearLayout$LayoutParams;)V
01-16 23:02:37.121 11917-11917/dhyna.inventory D/dalvikvm: VFY: replacing opcode 0x70 at 0x0000
01-16 23:02:37.171 11917-11917/dhyna.inventory V/Info: Nilaiid Intent1
01-16 23:02:37.171 11917-11917/dhyna.inventory D/AndroidRuntime: Shutting down VM
01-16 23:02:37.171 11917-11917/dhyna.inventory W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x40b05930)
01-16 23:02:37.191 11917-11917/dhyna.inventory E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{dhyna.inventory/dhyna.inventory.TampilDetilProduk}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at dhyna.inventory.TampilDetilProduk.tampilkanProduk(TampilDetilProduk.java:53)
at dhyna.inventory.TampilDetilProduk.onCreate(TampilDetilProduk.java:38)
at android.app.Activity.performCreate(Activity.java:5104)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
01-16 23:02:38.561 11917-11917/dhyna.inventory I/Process: Sending signal. PID: 11917 SIG: 9
对不起,如果我的英语和编码技巧如此糟糕。