-1

我在 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对每个列表视图行项执行操作。任何人都可以帮助我。谢谢。

4

2 回答 2

0

主要活动的布局文件是activity_main.xml:并且它没有一个元素作为名称为 delete_btn的按钮,但是有一个按钮作为view_member_entry.xml:的元素,

于 2014-12-22T15:23:43.217 回答
0

您正在加载activity_main活动并尝试delete_btn从那里获取。delete_btn但是该布局xml中没有任何内容。检查你的代码。

于 2014-12-22T15:25:20.367 回答