连接sqlite数据库后无法运行android应用程序

连接sqlite数据库后无法运行android应用程序,android,database,sqlite,android-layout,android-studio,Android,Database,Sqlite,Android Layout,Android Studio,我正在开发连接sqlite数据库的示例android应用程序。由于显示错误,我看不到它的真实界面。没有为它显示任何东西,有些时间显示也没有响应。它没有显示任何错误,但安卓设备显示图像也我添加我的代码如下。 Myactivity.java类 package com.example.randikawann.androidconnectsqlite; import android.support.v7.app.AppCompatActivity; import android.os.Bundle;

我正在开发连接sqlite数据库的示例android应用程序。由于显示错误,我看不到它的真实界面。没有为它显示任何东西,有些时间显示也没有响应。它没有显示任何错误,但安卓设备显示图像也我添加我的代码如下。 Myactivity.java类

package com.example.randikawann.androidconnectsqlite;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {
    EditText userInput;
    TextView userText;
    MyDBHandler myDBHandler;
    private static final String TAG = "MyActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        userInput = (EditText) findViewById(R.id.user_input);
        userText = (TextView) findViewById(R.id.user_text);
        myDBHandler=new MyDBHandler(this,null,null,1);
        printDatabase();
        Log.i(TAG, "start application");
    }

    public void addButtonClicked(View view){
        Products products=new Products(userInput.getText().toString());
        myDBHandler.addProduct(products);
        printDatabase();
        Log.i(TAG, "add button clicked");
    }

    public void deleteButtonClicked(View view){
        String inputText=userInput.getText().toString();
        myDBHandler.deleteproduct(inputText);
        printDatabase();
        Log.i(TAG, "delete button clicked");
    }
    private void printDatabase() {
        String dbString =myDBHandler.databaseToString();
        userText.setText(dbString);
        userInput.setText("");
        Log.i(TAG, "print database");
    }
    @Override
    protected void onDestroy() {
        myDBHandler.close();
        super.onDestroy();
    }
}
package com.example.randikawann.androidconnectsqlite;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class MyDBHandler extends SQLiteOpenHelper{
    private static final int DATABASE_VERSION=1;
    private static final String DATABASE_NAME="products.db";
    private static final String TABLE_PRODUCTS="products";
    private static final String COLUMN_ID="id";
    private static final String COLUMN_PRODUCTNAME="productname";

    public MyDBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version){
        super(context, DATABASE_NAME, factory,version);
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
        String query = "CREATE TABLE " + TABLE_PRODUCTS + "(" +
                COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                COLUMN_PRODUCTNAME + " TEXT " +
                ");";
        db.execSQL(query);

    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS "+ TABLE_PRODUCTS);
        onCreate(db);
    }
    //add a new row to database
    public void addProduct(Products products){
        SQLiteDatabase db=getWritableDatabase();
        ContentValues values=new ContentValues();
        values.put(COLUMN_PRODUCTNAME,products.get_productname());
        db.insert(TABLE_PRODUCTS,null,values);
        db.close();
    }
    //delect a row in database
    public void deleteproduct(String productName){
        SQLiteDatabase db=getWritableDatabase();
        String query="DELETE FROM "+ TABLE_PRODUCTS + " WHERE " + COLUMN_PRODUCTNAME + "=\"" + productName +"\";" ;
        db.execSQL(query);
    }
    //printout database as a string
    public String databaseToString(){
        String dbString = "";
        SQLiteDatabase db = this.getWritableDatabase();
        String query="SELECT * FROM " + TABLE_PRODUCTS + " WHERE 1";


        //cursor point to  a location in your result
        Cursor c=db.rawQuery(query, null);
        //move to first row in your results
        c.moveToFirst();

        while (!c.isAfterLast()){
            if (c.getString(c.getColumnIndex("productname"))!=null){
                dbString +=c.getString(c.getColumnIndex("productname"));
                dbString +="\n";
            }
        }
        db.close();
        return dbString;
    }




}
这是MyDBHandler.java类

package com.example.randikawann.androidconnectsqlite;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {
    EditText userInput;
    TextView userText;
    MyDBHandler myDBHandler;
    private static final String TAG = "MyActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        userInput = (EditText) findViewById(R.id.user_input);
        userText = (TextView) findViewById(R.id.user_text);
        myDBHandler=new MyDBHandler(this,null,null,1);
        printDatabase();
        Log.i(TAG, "start application");
    }

    public void addButtonClicked(View view){
        Products products=new Products(userInput.getText().toString());
        myDBHandler.addProduct(products);
        printDatabase();
        Log.i(TAG, "add button clicked");
    }

    public void deleteButtonClicked(View view){
        String inputText=userInput.getText().toString();
        myDBHandler.deleteproduct(inputText);
        printDatabase();
        Log.i(TAG, "delete button clicked");
    }
    private void printDatabase() {
        String dbString =myDBHandler.databaseToString();
        userText.setText(dbString);
        userInput.setText("");
        Log.i(TAG, "print database");
    }
    @Override
    protected void onDestroy() {
        myDBHandler.close();
        super.onDestroy();
    }
}
package com.example.randikawann.androidconnectsqlite;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class MyDBHandler extends SQLiteOpenHelper{
    private static final int DATABASE_VERSION=1;
    private static final String DATABASE_NAME="products.db";
    private static final String TABLE_PRODUCTS="products";
    private static final String COLUMN_ID="id";
    private static final String COLUMN_PRODUCTNAME="productname";

    public MyDBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version){
        super(context, DATABASE_NAME, factory,version);
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
        String query = "CREATE TABLE " + TABLE_PRODUCTS + "(" +
                COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                COLUMN_PRODUCTNAME + " TEXT " +
                ");";
        db.execSQL(query);

    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS "+ TABLE_PRODUCTS);
        onCreate(db);
    }
    //add a new row to database
    public void addProduct(Products products){
        SQLiteDatabase db=getWritableDatabase();
        ContentValues values=new ContentValues();
        values.put(COLUMN_PRODUCTNAME,products.get_productname());
        db.insert(TABLE_PRODUCTS,null,values);
        db.close();
    }
    //delect a row in database
    public void deleteproduct(String productName){
        SQLiteDatabase db=getWritableDatabase();
        String query="DELETE FROM "+ TABLE_PRODUCTS + " WHERE " + COLUMN_PRODUCTNAME + "=\"" + productName +"\";" ;
        db.execSQL(query);
    }
    //printout database as a string
    public String databaseToString(){
        String dbString = "";
        SQLiteDatabase db = this.getWritableDatabase();
        String query="SELECT * FROM " + TABLE_PRODUCTS + " WHERE 1";


        //cursor point to  a location in your result
        Cursor c=db.rawQuery(query, null);
        //move to first row in your results
        c.moveToFirst();

        while (!c.isAfterLast()){
            if (c.getString(c.getColumnIndex("productname"))!=null){
                dbString +=c.getString(c.getColumnIndex("productname"));
                dbString +="\n";
            }
        }
        db.close();
        return dbString;
    }




}
java类是

package com.example.randikawann.androidconnectsqlite;

public class Products {
    private int _id;
    private String _productname;

    public Products() {
    }

    public Products(String productname) {
        this._productname = productname;
    }

    public int get_id() {
        return _id;
    }

    public String get_productname() {
        return _productname;
    }

    public void set_id(int _id) {
        this._id = _id;
    }

    public void set_productname(String _productname) {
        this._productname = _productname;
    }
}
我还将xml文件调用添加为activity main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/user_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="150dp"
        android:layout_marginStart="150dp"
        android:layout_marginTop="250dp"
        android:text="Hello World!"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/add_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="50dp"
        android:layout_marginStart="50dp"
        android:layout_marginTop="150dp"
        android:onClick="addButtonClicked"
        android:text="Add"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/delete_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="50dp"
        android:layout_marginRight="50dp"
        android:layout_marginTop="150dp"
        android:onClick="deleteButtonClicked"
        android:text="Delete"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/user_input"
        android:layout_width="262dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="62dp"
        android:layout_marginStart="62dp"
        android:layout_marginTop="86dp"
        android:ems="10"
        android:inputType="textPersonName"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

我认为错误显示在down语句中。请告诉我如何解决这个问题

        while (!c.isAfterLast()){
            if (c.getString(c.getColumnIndex("productname"))!=null){
                dbString +=c.getString(c.getColumnIndex("productname"));
                dbString +="\n";
            }else{
                Log.i(TAG,"this is in else");

//            }
        }