0

当我运行以下示例时,“添加”按钮和“删除”按钮在点击时没有显示任何错误,但是,当我点击“显示”按钮时,我收到错误 - “不幸的是,学习已停止。”

这是我的 mainactivity.java 代码-

package com.example.learn;


import com.example.timepass.R;

import android.os.Bundle;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {

    Button b1, b2,b3;
    EditText t1,t2;

    SQLiteDatabase db;
    Cursor c;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        b1 = (Button) findViewById(R.id.button1);
        b2 = (Button) findViewById(R.id.button2);
        b3 = (Button) findViewById(R.id.button3);
        t1 = (EditText) findViewById(R.id.editText1);
        t2 = (EditText) findViewById(R.id.editText2);


        db = openOrCreateDatabase("MyDB", MODE_PRIVATE, null);
        db.execSQL("CREATE TABLE IF NOT EXISTS Names(Name VARCHAR)");

        b1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub

                if (t1.getText().toString().equals("")) {
                    t1.setText("Enter Name");
                }

                else {
                    String input = t1.getText().toString();
                    db.execSQL("INSERT INTO Names VALUES(" + "'" + input + "'"
                            + ")");
                }

            }
        });


        b2.setOnClickListener(new OnClickListener() {


            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                c = db.rawQuery("SELECT * FROM Names",null);
                int count = c.getCount();

                t2.setText(count);



              }
        });

        b3.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                db.execSQL("DELETE FROM Names");
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}

这是activity_main.xml代码-

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:ems="10"
        android:hint="Add" >

    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="19dp"
        android:text="@string/add" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/button1"
        android:layout_marginTop="36dp"
        android:ems="10"
        android:hint="Result" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button1"
        android:layout_centerVertical="true"
        android:text="@string/show" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button2"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="38dp"
        android:text="@string/del" />

</RelativeLayout>

我正在学习android,所以不太了解它,请帮助。为什么我会收到错误消息?

4

1 回答 1

0

这里:t2.setText(count); 这将崩溃,因为您试图将整数设置为文本并且框架需要有效的资源 id.. 所以您应该更改为 this(a String):t2.setText(String.valueOf(count));

于 2013-01-27T21:24:16.510 回答