真心蒙圈了 求指导啊::::
问题 btn 没事
btn_1会报错
假如 MainActivity中 的getMyAcitivty return this;
错误如下:
问题 btn 没事
btn_1会报错
假如 MainActivity中 的getMyAcitivty return this;
错误如下:
06-29 11:33:35.375: W/dalvikvm(1488): threadid=1: thread exiting with uncaught exception (group=0x415bdd58) 06-29 11:33:35.375: E/AndroidRuntime(1488): FATAL EXCEPTION: main 06-29 11:33:35.375: E/AndroidRuntime(1488): Process: a114.yangming.sqlite_4, PID: 1488 06-29 11:33:35.375: E/AndroidRuntime(1488): java.lang.NullPointerException 06-29 11:33:35.375: E/AndroidRuntime(1488): at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:256) 06-29 11:33:35.375: E/AndroidRuntime(1488): at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224) 06-29 11:33:35.375: E/AndroidRuntime(1488): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164) 06-29 11:33:35.375: E/AndroidRuntime(1488): at a114.yangming.db.Test.<init>(Test.java:15) 06-29 11:33:35.375: E/AndroidRuntime(1488): at a114.yangming.sqlite_4.MainActivity.onClick(MainActivity.java:69) 06-29 11:33:35.375: E/AndroidRuntime(1488): at android.view.View.performClick(View.java:4438) 06-29 11:33:35.375: E/AndroidRuntime(1488): at android.view.View$PerformClick.run(View.java:18424) 06-29 11:33:35.375: E/AndroidRuntime(1488): at android.os.Handler.handleCallback(Handler.java:733) 06-29 11:33:35.375: E/AndroidRuntime(1488): at android.os.Handler.dispatchMessage(Handler.java:95) 06-29 11:33:35.375: E/AndroidRuntime(1488): at android.os.Looper.loop(Looper.java:136) 06-29 11:33:35.375: E/AndroidRuntime(1488): at android.app.ActivityThread.main(ActivityThread.java:5137) 06-29 11:33:35.375: E/AndroidRuntime(1488): at java.lang.reflect.Method.invokeNative(Native Method) 06-29 11:33:35.375: E/AndroidRuntime(1488): at java.lang.reflect.Method.invoke(Method.java:515) 06-29 11:33:35.375: E/AndroidRuntime(1488): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:801) 06-29 11:33:35.375: E/AndroidRuntime(1488): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:617) 06-29 11:33:35.375: E/AndroidRuntime(1488): at dalvik.system.NativeStart.main(Native Method)
假如 return getBaseContext();
错误如下:
06-29 12:03:03.005: E/AndroidRuntime(27447): FATAL EXCEPTION: main 06-29 12:03:03.005: E/AndroidRuntime(27447): Process: a114.yangming.sqlite_4, PID: 27447 06-29 12:03:03.005: E/AndroidRuntime(27447): java.lang.NullPointerException 06-29 12:03:03.005: E/AndroidRuntime(27447): at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224) 06-29 12:03:03.005: E/AndroidRuntime(27447): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164) 06-29 12:03:03.005: E/AndroidRuntime(27447): at a114.yangming.db.Test.<init>(Test.java:16) 06-29 12:03:03.005: E/AndroidRuntime(27447): at a114.yangming.sqlite_4.MainActivity.onClick(MainActivity.java:71) 06-29 12:03:03.005: E/AndroidRuntime(27447): at android.view.View.performClick(View.java:4438) 06-29 12:03:03.005: E/AndroidRuntime(27447): at android.view.View$PerformClick.run(View.java:18424) 06-29 12:03:03.005: E/AndroidRuntime(27447): at android.os.Handler.handleCallback(Handler.java:733) 06-29 12:03:03.005: E/AndroidRuntime(27447): at android.os.Handler.dispatchMessage(Handler.java:95) 06-29 12:03:03.005: E/AndroidRuntime(27447): at android.os.Looper.loop(Looper.java:136) 06-29 12:03:03.005: E/AndroidRuntime(27447): at android.app.ActivityThread.main(ActivityThread.java:5137) 06-29 12:03:03.005: E/AndroidRuntime(27447): at java.lang.reflect.Method.invokeNative(Native Method) 06-29 12:03:03.005: E/AndroidRuntime(27447): at java.lang.reflect.Method.invoke(Method.java:515) 06-29 12:03:03.005: E/AndroidRuntime(27447): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:801) 06-29 12:03:03.005: E/AndroidRuntime(27447): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:617) 06-29 12:03:03.005: E/AndroidRuntime(27447): at dalvik.system.NativeStart.main(Native Method)
代码:
MainActivity
package a114.yangming.sqlite_4; import a114.yangming.db.MySQLiteOpenHelper; import a114.yangming.db.Test; import android.app.Activity; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity implements OnClickListener { private Button btn,btn_1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); } private void init() { btn = (Button) findViewById(R.id.btn_connect); btn_1=(Button) findViewById(R.id.btn_connect_1); btn_1.setOnClickListener(this); btn.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_connect: SQLiteDatabase db=new MySQLiteOpenHelper(this, "测试.db", 1).getWritableDatabase(); db.close(); break; case R.id.btn_connect_1: new Test(); break; } } public Context getMyContext() { // return this; return getBaseContext(); } }
MySQLiteOpenHelper
/** * @作者 杨铭; * @时间 2015年6月29日_上午10:52:23; * @联系qq <771365380>\<286384491>; */ package a114.yangming.db; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class MySQLiteOpenHelper extends SQLiteOpenHelper { public MySQLiteOpenHelper(Context context, String name, int version) { super(context, name, null, version, null); } @Override public void onCreate(SQLiteDatabase db) { String sql ="CREATE TABLE test (id INTEGER PRIMARY KEY AUTOINCREMENT , name , phone );"; db.execSQL(sql); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub } }
Test
package a114.yangming.db; import a114.yangming.sqlite_4.MainActivity; import android.database.sqlite.SQLiteDatabase; public class Test { public Test() { SQLiteDatabase db=new MySQLiteOpenHelper(new MainActivity().getMyContext(), "Test", 1).getWritableDatabase(); db.close(); } }
解决方案
20
new MainActivity()
Activity怎么能本人创建呢?
都是由android系统创建的?
里面需要很多初始化的东西,
Activity怎么能本人创建呢?
都是由android系统创建的?
里面需要很多初始化的东西,
20