我是Java和Android的新手当我尝试将我的Android应用程序连接到SQLite时,每次在应用程序调用数据库帮助程序类上的onCreate方法时崩溃应用程序时都会产生空点错误。
我无法在代码中找到错误,所以请有人帮助我。
MainActivity.java代码:
package com.example.sdreddy.dbfinal;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
DbNeed dbn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button dd=(Button)findViewById(R.id.button);
dd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
TextView tv = (TextView) findViewById(R.id.textView);
try {
dbn.Addd("SDREDDY");
String zz = dbn.getdata();
tv.setText(zz);
}
catch (Exception ee){
tv.setText((CharSequence) ee.toString());
}
}
});
}
}
DbNeed.java代码
package com.example.sdreddy.dbfinal;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.Random;
import java.util.concurrent.CountDownLatch;
/**
* Created by SDREDDY on 26-04-2018.
*/
public class DbNeed extends SQLiteOpenHelper {
public static final String dbname="Database.db";
public static final String tblname="MyTable";
public DbNeed(Context context) {
super(context, dbname, null, 1);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL("create table if not exists "+tblname+" (id text,value text)");
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
sqLiteDatabase.execSQL("drop table if exists "+tblname);
onCreate(sqLiteDatabase);
}
public void Addd(String Name){
SQLiteDatabase sqLiteDatabase=this.getWritableDatabase();
ContentValues cv=new ContentValues();
Random r=new Random();
String idz=String.valueOf(r.nextInt());
cv.put("id",idz);
cv.put("name",Name);
sqLiteDatabase.insert(tblname,null,cv);
sqLiteDatabase.close();
}
public String getdata(){
SQLiteDatabase sqLiteDatabase=this.getReadableDatabase();
String qu="SELECT * FROM "+tblname;
Cursor crz=sqLiteDatabase.rawQuery(qu,null);
sqLiteDatabase.close();
String sim=crz.getString(0);
return sim;
}
}
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="com.example.sdreddy.dbfinal.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="Name" />
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView"
android:textSize="30sp" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
答案 0 :(得分:0)
在行setContentView(R.layout.activity_main);
之后添加以下内容: -
dbn = new DbNeed(this);
这将实例化(设置) dbn
,它在此阶段仅被声明,因此为空。