我在 delete_btn 处收到一个空指针异常。我在下面的代码中提到了错误行,并且我将相关的代码发布到该代码中。
堆栈跟踪:
12-22 10:03:59.897: E/AndroidRuntime(1505): FATAL EXCEPTION: main
12-22 10:03:59.897: E/AndroidRuntime(1505): Process: com.example.sqlitedemoo, PID: 1505
12-22 10:03:59.897: E/AndroidRuntime(1505): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sqlitedemoo/com.pavan.sqlitedemoo.MainActivity}: java.lang.NullPointerException
12-22 10:03:59.897: E/AndroidRuntime(1505): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
12-22 10:03:59.897: E/AndroidRuntime(1505): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
12-22 10:03:59.897: E/AndroidRuntime(1505): at android.app.ActivityThread.access$800(ActivityThread.java:135)
12-22 10:03:59.897: E/AndroidRuntime(1505): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
12-22 10:03:59.897: E/AndroidRuntime(1505): at android.os.Handler.dispatchMessage(Handler.java:102)
12-22 10:03:59.897: E/AndroidRuntime(1505): at android.os.Looper.loop(Looper.java:136)
12-22 10:03:59.897: E/AndroidRuntime(1505): at android.app.ActivityThread.main(ActivityThread.java:5017)
12-22 10:03:59.897: E/AndroidRuntime(1505): at java.lang.reflect.Method.invokeNative(Native Method)
12-22 10:03:59.897: E/AndroidRuntime(1505): at java.lang.reflect.Method.invoke(Method.java:515)
12-22 10:03:59.897: E/AndroidRuntime(1505): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
12-22 10:03:59.897: E/AndroidRuntime(1505): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
12-22 10:03:59.897: E/AndroidRuntime(1505): at dalvik.system.NativeStart.main(Native Method)
12-22 10:03:59.897: E/AndroidRuntime(1505): Caused by: java.lang.NullPointerException
12-22 10:03:59.897: E/AndroidRuntime(1505): at com.pavan.sqlitedemoo.MainActivity.onCreate(MainActivity.java:50)
12-22 10:03:59.897: E/AndroidRuntime(1505): at android.app.Activity.performCreate(Activity.java:5231)
12-22 10:03:59.897: E/AndroidRuntime(1505): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
12-22 10:03:59.897: E/AndroidRuntime(1505): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
12-22 10:03:59.897: E/AndroidRuntime(1505): ... 11 more
MainActivity.java:
public class MainActivity extends Activity {
ListView lv;
SQLController dbcon;
TextView memID_tv, memName_tv;
Button delete_btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbcon = new SQLController(this);
dbcon.open();
lv = (ListView) findViewById(R.id.memberList_id);
delete_btn = (Button) findViewById(R.id.delete_btn);
// Attach The Data From DataBase Into ListView Using Crusor Adapter
Cursor cursor = dbcon.readData();
String[] from = new String[] { DBhelper.MEMBER_ID, DBhelper.MEMBER_NAME };
int[] to = new int[] { R.id.member_id, R.id.member_name };
@SuppressWarnings("deprecation")
SimpleCursorAdapter adapter = new SimpleCursorAdapter(
MainActivity.this, R.layout.view_member_entry, cursor, from, to);
adapter.notifyDataSetChanged();
lv.setAdapter(adapter);
delete_btn.setOnClickListener(new OnClickListener() { --> 50th Line- Null pointer Exception
@Override
public void onClick(View v) {
memID_tv = (TextView) v.findViewById(R.id.member_id);
memName_tv = (TextView) v.findViewById(R.id.member_name);
String memberID_val = memID_tv.getText().toString();
String memberName_val = memName_tv.getText().toString();
Intent modify_intent = new Intent(getApplicationContext(),
Modify_member.class);
modify_intent.putExtra("memberName", memberName_val);
modify_intent.putExtra("memberID", memberID_val);
startActivity(modify_intent);
}
});
}
我需要的:
我Delete
在 adaper xml(view_member_entry.xml) 中添加了按钮。因为我需要delete
对每个列表视图行项执行操作。任何人都可以帮助我。谢谢。