Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用androidsqlite数据库_Android_Sqlite - Fatal编程技术网

如何使用androidsqlite数据库

如何使用androidsqlite数据库,android,sqlite,Android,Sqlite,当我运行下面的示例时,“添加”按钮和“删除”按钮在点击时没有显示任何错误,但是,当我点击“显示”按钮时,我得到了错误——“不幸的是,学习已停止。” 这是我的mainactivity.java代码- package com.example.learn; import com.example.timepass.R; import android.os.Bundle; import android.app.Activity; import android.database.Cursor; imp

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

这是我的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,所以不太了解它,请帮忙。为什么会出现错误?

这里:
t2.setText(count)

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

每当发生崩溃时,发布堆栈跟踪。我应该发布日志猫还是错误日志?是的,你应该将日志发布在崩溃的地方。。这里还有:t2.setText(count);这也会崩溃,因为您试图将文本设置为整数,并且框架需要有效的资源id。。所以您应该改为:t2.setText(String.valueOf(count))@Cata谢谢你,它现在起作用了。很高兴它起到了作用,我把它转换成了一个答案。。如果您愿意,您可以接受它作为一个答案:)