尝试了很多搜索,所提供的解决方案是将活动添加到清单,将类添加到清单等等。没有得到任何解决方案。所以请帮助。
通过以下在线教程学习sqlite数据库访问。尝试了以下操作并在logcat中获得了很多错误。
支持DbHelper类
public class DbHelper extends SQLiteOpenHelper {
private static final String Name = "Record.db";
private static final int Version = 1;
public DbHelper(Context context){
super(context,Name,null,Version);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("create table vijay(name text, id integer primary key)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("drop table if exists vijay");
onCreate(db);
}
public void addRecord(String name, int id){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("name",name);
values.put("id", id);
db.insert("vijay", null, values);
db.close();
}
}
主要活动类
public class MainActivity extends Activity {
DbHelper dbhelper = new DbHelper(getApplicationContext());
EditText et1;
EditText et2;
String s;
int ss;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et1 = (EditText)findViewById(R.id.editText1);
et2 = (EditText)findViewById(R.id.editText2);
s = et1.getText().toString();
ss = Integer.parseInt(et2.getText().toString());
}
public void add(View v){
dbhelper.addRecord(s,ss);
et1.setText("");
et2.setText("");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
android manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.databaseaccess"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.databaseaccess.MainActivity"
android:label="@string/app_name"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Logcat显示许多错误
07-29 16:48:56.793: E/AndroidRuntime(870): FATAL EXCEPTION: main
07-29 16:48:56.793: E/AndroidRuntime(870): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.databaseaccess/com.example.databaseaccess.MainActivity}: java.lang.NullPointerException
07-29 16:48:56.793: E/AndroidRuntime(870): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
07-29 16:48:56.793: E/AndroidRuntime(870): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-29 16:48:56.793: E/AndroidRuntime(870): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-29 16:48:56.793: E/AndroidRuntime(870): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-29 16:48:56.793: E/AndroidRuntime(870): at android.os.Handler.dispatchMessage(Handler.java:99)
07-29 16:48:56.793: E/AndroidRuntime(870): at android.os.Looper.loop(Looper.java:123)
07-29 16:48:56.793: E/AndroidRuntime(870): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-29 16:48:56.793: E/AndroidRuntime(870): at java.lang.reflect.Method.invokeNative(Native Method)
07-29 16:48:56.793: E/AndroidRuntime(870): at java.lang.reflect.Method.invoke(Method.java:521)
07-29 16:48:56.793: E/AndroidRuntime(870): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-29 16:48:56.793: E/AndroidRuntime(870): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-29 16:48:56.793: E/AndroidRuntime(870): at dalvik.system.NativeStart.main(Native Method)
07-29 16:48:56.793: E/AndroidRuntime(870): Caused by: java.lang.NullPointerException
07-29 16:48:56.793: E/AndroidRuntime(870): at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:100)
07-29 16:48:56.793: E/AndroidRuntime(870): at com.example.databaseaccess.MainActivity.<init>(MainActivity.java:11)
07-29 16:48:56.793: E/AndroidRuntime(870): at java.lang.Class.newInstanceImpl(Native Method)
07-29 16:48:56.793: E/AndroidRuntime(870): at java.lang.Class.newInstance(Class.java:1429)
07-29 16:48:56.793: E/AndroidRuntime(870): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
07-29 16:48:56.793: E/AndroidRuntime(870): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
07-29 16:48:56.793: E/AndroidRuntime(870): ... 11 more
答案 0 :(得分:2)
将以下行移至OnCreate
方法
DbHelper dbhelper = new DbHelper(getApplicationContext());
答案 1 :(得分:0)
为DBHelper创建全局变量....
像这样:
DbHelper dbhelper ;
然后在onCreate()方法中为它创建一个对象:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbhelper = new DbHelper(MainActivity.this);
}
答案 2 :(得分:-1)
我发现另一个导致应用程序崩溃的错误。
作为Shani&amp; Abbas提到(感谢他们),在onCreate方法中实例化类。
edittext字段的值在输入某些值之前存储在onCreate方法本身的String和int变量中。现在摆脱了它们。将它们放在按钮'onclick事件功能。
感谢所有......