iT邦幫忙

0

Android Studio SQLite新增更改刪除 閃退求解

  • 分享至 

  • xImage

各位大大晚上好
我最近嘗試摸索Android Studio
遇到一些問題 想請教一下大家

我目前在試SQLite 新增的部分沒有什麼問題
但在修改、刪除和選取上就都會閃退

以下是我的宣告
public SQL DH = null;
public SQLiteDatabase db;
public ListView LV1;
String id_text=null;
private Button b1;
private Button b2;
private Button b3;
EditText f1;
EditText c1;

**以下為修改的程式碼**
b2 = (Button)findViewById(R.id.b2);
    b2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            update(id_text,f1.getText().toString(),c1.getText().toString());
            select();

        }
    });
    
    private void update(String id,String s,String n){
    ContentValues values = new ContentValues();
    values.put("_name",s.toString());
    values.put("_cal",n.toString());
    db.update("TB1",values,"_id+"+id_text,null);


**以下為刪除的程式碼**
b3 = (Button)findViewById(R.id.b3);
        b3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                del(id_text);
                select();
                id_text= null;

            }
        });
        
        private void del(String id)
{
db.delete("TB1","_id"+id,null);
}


**以下為選取的程式碼**
LV1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                TextView tx1 = (TextView)view.findViewById(R.id.tl1);
                id_text=tx1.getText().toString();
                TextView tx2 = (TextView)view.findViewById(R.id.tl2);
                f1.setText(tx2.getText().toString());
                TextView tx3 = (TextView)view.findViewById(R.id.tl3);
                c1.setText(tx3.getText().toString());


            }
        });
        
        再麻煩各位指點小弟了 有缺資料再和我說
看更多先前的討論...收起先前的討論...
GHH iT邦新手 1 級 ‧ 2019-11-14 09:49:34 檢舉
閃退的 Log? select() 裡面寫了什麼?
jan530315 iT邦新手 5 級 ‧ 2019-11-17 18:17:36 檢舉
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.firstpage.setting, PID: 8034
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.setText(java.lang.CharSequence)' on a null object reference
at com.firstpage.setting.BActivity$4.onItemClick(BActivity.java:127)
at android.widget.AdapterView.performItemClick(AdapterView.java:318)
at android.widget.AbsListView.performItemClick(AbsListView.java:1159)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3136)
at android.widget.AbsListView$3.run(AbsListView.java:4052)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
這是閃退的LOG
jan530315 iT邦新手 5 級 ‧ 2019-11-17 18:18:41 檢舉
LV1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView tx1 = (TextView)view.findViewById(R.id.tl1);
id_text=tx1.getText().toString();
TextView tx2 = (TextView)view.findViewById(R.id.tl2);
f1.setText(tx2.getText().toString()); <------------這是錯誤訊息的127行
TextView tx3 = (TextView)view.findViewById(R.id.tl3);
c1.setText(tx3.getText().toString());


}
});
jan530315 iT邦新手 5 級 ‧ 2019-11-17 18:19:11 檢舉
private void select(){
Cursor cursor = db.query("TB1",new String[]{"_id","_name","_cal"},null,null,null,null,null);
List<Map<String,Object>> items=new ArrayList<Map<String, Object>>();
cursor.moveToFirst();
for (int i=0;i<cursor.getCount();i++)
{
Map<String,Object> item = new HashMap<String, Object>();
item.put("_id",cursor.getString(0));
item.put("_name",cursor.getString(1));
item.put("_cal",cursor.getString(2));
items.add(item);
cursor.moveToNext();
}
SimpleAdapter SA = new SimpleAdapter(this,items,R.layout.layout,new String[]{"_id","_name","_cal"},new int[]{R.id.tl1,R.id.tl2,R.id.tl3});
LV1.setAdapter(SA);
}
這是SELECT()


再麻煩您指點
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友回答

立即登入回答