我是android的新手,我会从TextView中的现有数据库中检索数据。我正在尝试从这个开始
public class MainActivity extends ListActivity {
private static final String DB_NAME = "MN1";
private static final String TABLE_NAME = "MN1";
public static final String KEY_CONTACTID = "_id";
public static final String KEY_NUM = "Numero";
TextView textView1 = (TextView)findViewById(R.id.textView1);
private SQLiteDatabase database;
private Cursor cursor;
private ContentValues createContentValues(String Numero, String Nome) {
ContentValues values = new ContentValues();
return values;
}
public Cursor fetchAllContacts() {
return database.query(TABLE_NAME, new String[] { KEY_CONTACTID, KEY_NUM, KEY_NOME}, null, null);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ExternalDbOpenHelper dbOpenHelper = new ExternalDbOpenHelper(this, DB_NAME);
database = dbOpenHelper.openDataBase();
startManagingCursor(cursor);
while ( cursor.moveToNext() ) {
String Numero = cursor.getString( cursor.getColumnIndex(this.KEY_CONTACTID) );
textView1.setText(Numero);
}
cursor.close();
}
但它给出了一个nullpointer exeption,我在哪里做错了?
这是logcat
03-09 12:16:12.544: D/AndroidRuntime(416): Shutting down VM
03-09 12:16:12.544: W/dalvikvm(416): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
03-09 12:16:12.614: E/AndroidRuntime(416): FATAL EXCEPTION: main
03-09 12:16:12.614: E/AndroidRuntime(416): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.provadatabaaaas/com.example.provadatabaaaas.MainActivity}: java.lang.NullPointerException
03-09 12:16:12.614: E/AndroidRuntime(416): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
03-09 12:16:12.614: E/AndroidRuntime(416): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
03-09 12:16:12.614: E/AndroidRuntime(416): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
03-09 12:16:12.614: E/AndroidRuntime(416): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
03-09 12:16:12.614: E/AndroidRuntime(416): at android.os.Handler.dispatchMessage(Handler.java:99)
03-09 12:16:12.614: E/AndroidRuntime(416): at android.os.Looper.loop(Looper.java:123)
03-09 12:16:12.614: E/AndroidRuntime(416): at android.app.ActivityThread.main(ActivityThread.java:4627)
03-09 12:16:12.614: E/AndroidRuntime(416): at java.lang.reflect.Method.invokeNative(Native Method)
03-09 12:16:12.614: E/AndroidRuntime(416): at java.lang.reflect.Method.invoke(Method.java:521)
03-09 12:16:12.614: E/AndroidRuntime(416): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-09 12:16:12.614: E/AndroidRuntime(416): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-09 12:16:12.614: E/AndroidRuntime(416): at dalvik.system.NativeStart.main(Native Method)
03-09 12:16:12.614: E/AndroidRuntime(416): Caused by: java.lang.NullPointerException
03-09 12:16:12.614: E/AndroidRuntime(416): at android.app.Activity.findViewById(Activity.java:1637)
03-09 12:16:12.614: E/AndroidRuntime(416): at com.example.provadatabaaaas.MainActivity.<init>(MainActivity.java:30)
03-09 12:16:12.614: E/AndroidRuntime(416): at java.lang.Class.newInstanceImpl(Native Method)
03-09 12:16:12.614: E/AndroidRuntime(416): at java.lang.Class.newInstance(Class.java:1429)
03-09 12:16:12.614: E/AndroidRuntime(416): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
03-09 12:16:12.614: E/AndroidRuntime(416): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
03-09 12:16:12.614: E/AndroidRuntime(416): ... 11 more
03-09 12:16:15.843: I/Process(416): Sending signal. PID: 416 SIG: 9
和布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="TextView" />
</RelativeLayout>
答案 0 :(得分:2)
首先纠正此帖
TextView textView1 = (TextView)findViewById(R.id.textView1);
在OnCreate()
setContentView(R.layout.activity_main);
内