请告诉我下面写的代码有什么问题

时间:2016-10-05 17:25:39

标签: android

我编写了一段代码,用于填充数据库。但是,当我在我的Android设备或模拟器中执行该代码时,它会出现错误“抱歉app1已停止工作”请帮我找出错误。

MainActivity.java文件

    package cu_coders.app1;

import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.common.api.GoogleApiClient;

public class MainActivity extends AppCompatActivity {
    public final EditText e=(EditText)findViewById((R.id.text1));
    public final file1 f=new file1();
    TextView t=(TextView)findViewById(R.id.textView1);
    final myDB x=new myDB(this, null, null, 1);

    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */
    private GoogleApiClient client;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
        Button enter=(Button)findViewById(R.id.button1);
        Button delete=(Button)findViewById(R.id.button2);
        enter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = e.getText().toString();
                f.set_name(s);
                x.addItem(f);
                t.setText(x.printTable());
            }
        });
    }
}

CLASS NAMED FILE1.JAVA

package cu_coders.app1;

/**
 * Created by user pc on 03-10-2016.
 */
public class file1 {
    private int _roll;
    private String _name;

    public file1() {
    }

    public file1(String _name) {
        this._name = _name;
    }

    public int get_roll() {
        return _roll;
    }

    public String get_name() {
        return _name;
    }

    public void set_roll(int _roll) {
        this._roll = _roll;
    }

    public void set_name(String _name) {
        this._name = _name;
    }
}

数据库管理类 - myDB.java

package cu_coders.app1;

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

/**
 * Created by user pc on 03-10-2016.
 */
public class myDB extends SQLiteOpenHelper {
    private static int DATABASE_VERSION=1;
    private static String DATABASE_NAME="file.db";
    private static String TABLE_FILE1="class_cse";
    public static String COLUMN_KEY="_key";
    public static String COLUMN_NAME="_name";

    public myDB(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, DATABASE_NAME, factory, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String query="create table "+ TABLE_FILE1 + "(" +
                COLUMN_KEY + " INTEGER PRIMARY KEY "+
                COLUMN_NAME+" TEXT "+
                ");";
        db.execSQL(query);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_FILE1+";");
        onCreate(db);

    }
    public void addItem(file1 f){
        ContentValues cv=new ContentValues();
        cv.put(COLUMN_NAME,f.get_name());
        SQLiteDatabase db=getWritableDatabase();
        db.insert(TABLE_FILE1,null,cv);
        db.close();
    }
    /*public void deletItem(String item){
        SQLiteDatabase db=getWritableDatabase();
        db.execSQL("DELETE FROM "+TABLE_FILE1+" WHERE "+COLUMN_NAME+" =\" "+ item +"\";");
        db.close();
    } */
    public String printTable(){
        String dbstring="";
        SQLiteDatabase db=getWritableDatabase();
        String query="SELECT * FROM "+TABLE_FILE1+" WHERE 1;";
        Cursor c=db.rawQuery(query,null);
        c.moveToFirst();
        while(!c.moveToLast()){
            if(c.getString(c.getColumnIndex(COLUMN_NAME))!=null) {
                dbstring += c.getString(c.getColumnIndex(COLUMN_NAME));
                dbstring += '\n';
            }
        }
        db.close();
        return dbstring;
    }
}
请帮助我找出问题所在。提前谢谢你。

1 个答案:

答案 0 :(得分:1)

你应该改变几个项目我猜...

将myDB构造函数更改为:

public myDB(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

并在mainActivity类中更改所有全局变量:

 private EditText e ;
 private  file1 f ;
 private  TextView t;

并在oncreate()中初始化它们:

 e=(EditText)findViewById((R.id.text1));
 f=new file1();
 t=(TextView)findViewById(R.id.textView1);