使用按钮执行android数据库查询

时间:2014-12-03 03:31:34

标签: java android

我正在尝试创建一个按钮,每次点击时都会执行搜索查询。查询将从数据库中获取5个随机条目,并将其显示在列表视图中。

public abstract class MainActivity extends ListActivity implements OnClickListener {

    private Cursor schedule;
    private MyDatabase db;
    ListView listView;
    Button b1 =(Button) findViewById(android.R.id.button1);

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        db = new MyDatabase(this);
        schedule = db.getSchedule();

        b1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                ListView listView = (ListView) findViewById(android.R.id.list);
                listView.setAdapter(new SimpleCursorAdapter(
                        MainActivity.this, 
                        R.layout.row,  
                        schedule,                   
                        new String[] {"fName", "Calories", "Protein", "Carbs" },  
                        new int[] { R.id.textView1, R.id.textView2, R.id.textView3, R.id.textView4 },   
                        0));
            }
        });
    }

目录下载:

12-03 00:22:51.013: D/dalvikvm(2900): newInstance failed: p0 i0 [0 a1
12-03 00:22:51.013: D/AndroidRuntime(2900): Shutting down VM
12-03 00:22:51.013: W/dalvikvm(2900): threadid=1: thread exiting with uncaught exception (group=0xb1a68ba8)
12-03 00:22:51.113: E/AndroidRuntime(2900): FATAL EXCEPTION: main
12-03 00:22:51.113: E/AndroidRuntime(2900): Process: com.example.mealplan, PID: 2900
12-03 00:22:51.113: E/AndroidRuntime(2900): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.mealplan/com.example.mealplan.MainActivity}: java.lang.InstantiationException: can't instantiate class com.example.mealplan.MainActivity
12-03 00:22:51.113: E/AndroidRuntime(2900):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
12-03 00:22:51.113: E/AndroidRuntime(2900):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
12-03 00:22:51.113: E/AndroidRuntime(2900):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
12-03 00:22:51.113: E/AndroidRuntime(2900):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
12-03 00:22:51.113: E/AndroidRuntime(2900):     at android.os.Handler.dispatchMessage(Handler.java:102)
12-03 00:22:51.113: E/AndroidRuntime(2900):     at android.os.Looper.loop(Looper.java:136)
12-03 00:22:51.113: E/AndroidRuntime(2900):     at android.app.ActivityThread.main(ActivityThread.java:5017)
12-03 00:22:51.113: E/AndroidRuntime(2900):     at java.lang.reflect.Method.invokeNative(Native Method)
12-03 00:22:51.113: E/AndroidRuntime(2900):     at java.lang.reflect.Method.invoke(Method.java:515)
12-03 00:22:51.113: E/AndroidRuntime(2900):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
12-03 00:22:51.113: E/AndroidRuntime(2900):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
12-03 00:22:51.113: E/AndroidRuntime(2900):     at dalvik.system.NativeStart.main(Native Method)
12-03 00:22:51.113: E/AndroidRuntime(2900): Caused by: java.lang.InstantiationException: can't instantiate class com.example.mealplan.MainActivity
12-03 00:22:51.113: E/AndroidRuntime(2900):     at java.lang.Class.newInstanceImpl(Native Method)
12-03 00:22:51.113: E/AndroidRuntime(2900):     at java.lang.Class.newInstance(Class.java:1208)
12-03 00:22:51.113: E/AndroidRuntime(2900):     at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
12-03 00:22:51.113: E/AndroidRuntime(2900):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)

我为问题添加了一个catlog,并插入了建议,以便在我收到这些错误时显示我的内容。

3 个答案:

答案 0 :(得分:0)

请在您的代码中添加该行。 的 b1.setonclickListener(本);

没有写这行怎么调用按钮的onclick监听器方法?

我希望它对你有用。

修改

在setContentView()之后添加此行。

按钮b1 =(按钮)findViewById(android.R.id.button1);

答案 1 :(得分:0)

像这样更改你的代码!

public class MainActivity extends ListActivity implements OnClickListener {

    private Cursor schedule;
    private MyDatabase db;

    //declare the variables of the ListView and Button
    ListView listView;
    Button b1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        db = new MyDatabase(this);
        schedule = db.getSchedule();

        //initialize the ListView and Button
        listView = (ListView) findViewById(android.R.id.list);
        b1 = (Button) findViewById(android.R.id.button1);

        b1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                listView.setAdapter(new SimpleCursorAdapter(
                    MainActivity.this, 
                        R.layout.row,  
                        schedule,                   
                        new String[] {"fName", "Calories", "Protein", "Carbs" },  
                        new int[] { R.id.textView1, R.id.textView2, R.id.textView3, R.id.textView4 },   
                        0));
            }
        });
    }

答案 2 :(得分:0)

can't instantiate class com.example.mealplan.MainActivity

无法实例化抽象类。从班级声明中删除abstract