2012-03-14 71 views
1

我花了一天的時間來試圖弄清楚這一點(不用說我的學習曲線很長 - 因此我需要尋求幫助!) - 下面是一個簡單的數據庫程序(我試圖瞭解他們如何工作來實現我正在工作的另一個應用程序)。無法證實活動ComponentInfo - 空指針異常

當我點擊 '顯示' 按鈕 - 我得到以下運行時錯誤:

登錄貓

03-14 16:13:39.612: ERROR/AndroidRuntime(341): FATAL EXCEPTION: main 
03-14 16:13:39.612: ERROR/AndroidRuntime(341): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.android.dbtest/com.android.dbtest.ShowActivity}: java.lang.NullPointerException 
03-14 16:13:39.612: ERROR/AndroidRuntime(341):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1544) 
03-14 16:13:39.612: ERROR/AndroidRuntime(341):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1638) 
03-14 16:13:39.612: ERROR/AndroidRuntime(341):  at android.app.ActivityThread.access$1500(ActivityThread.java:117) 
03-14 16:13:39.612: ERROR/AndroidRuntime(341):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:928) 
03-14 16:13:39.612: ERROR/AndroidRuntime(341):  at android.os.Handler.dispatchMessage(Handler.java:99) 
03-14 16:13:39.612: ERROR/AndroidRuntime(341):  at android.os.Looper.loop(Looper.java:123) 
03-14 16:13:39.612: ERROR/AndroidRuntime(341):  at android.app.ActivityThread.main(ActivityThread.java:3647) 
03-14 16:13:39.612: ERROR/AndroidRuntime(341):  at java.lang.reflect.Method.invokeNative(Native Method) 
03-14 16:13:39.612: ERROR/AndroidRuntime(341):  at java.lang.reflect.Method.invoke(Method.java:507) 
03-14 16:13:39.612: ERROR/AndroidRuntime(341):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
03-14 16:13:39.612: ERROR/AndroidRuntime(341):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
03-14 16:13:39.612: ERROR/AndroidRuntime(341):  at dalvik.system.NativeStart.main(Native Method) 
03-14 16:13:39.612: ERROR/AndroidRuntime(341): Caused by: java.lang.NullPointerException 
03-14 16:13:39.612: ERROR/AndroidRuntime(341):  at android.app.Activity.findViewById(Activity.java:1647) 
03-14 16:13:39.612: ERROR/AndroidRuntime(341):  at com.android.dbtest.ShowActivity.<init>(ShowActivity.java:12) 
03-14 16:13:39.612: ERROR/AndroidRuntime(341):  at java.lang.Class.newInstanceImpl(Native Method) 
03-14 16:13:39.612: ERROR/AndroidRuntime(341):  at java.lang.Class.newInstance(Class.java:1409) 
03-14 16:13:39.612: ERROR/AndroidRuntime(341):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021) 
03-14 16:13:39.612: ERROR/AndroidRuntime(341):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1536) 
03-14 16:13:39.612: ERROR/AndroidRuntime(341):  ... 11 more 

我的主要活動:

package com.android.dbtest; 




import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 


public class DatabaseTestActivity extends Activity { 
/** Called when the activity is first created. */ 


@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 


    } 

    public void handleClick(View v){ 
     Intent intent = new Intent(); 
     intent.setClass(this, AddActivity.class); 
     startActivity(intent); 
    } 

    public void showHandleClick(View v){ 
     Intent intent = new Intent(); 
     intent.setClass(this, ShowActivity.class); 
     startActivity(intent); 
    } 
    } 

我型我秀活動(此是我認爲主要錯誤(猜測多於一個)的原因可能在於,因爲它是當我按下顯示按鈕時發生運行時錯誤,我相信它與構建列表有關):

package com.android.dbtest; 

import android.database.Cursor; 
import android.os.Bundle; 
import android.widget.ListView; 
import android.widget.SimpleCursorAdapter; 
import android.util.Log; 

public class ShowActivity extends DatabaseTestActivity{ 

private SQLiteAdapter mySQLiteAdapter; 
ListView listContent = (ListView)findViewById(R.id.contentlist); 

//TextView listContent = (TextView) findViewById(R.id.contentlist); 

SimpleCursorAdapter cursorAdapter; 
String cursor; 

public static final String LOG_TAG = "dbtest"; 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.show); 
    // TODO Auto-generated method stub 

    Log.v(LOG_TAG, "step 1"); 


    //output.setText(String.valueOf(WHAT I GET FROM DATABASE)); 


    mySQLiteAdapter = new SQLiteAdapter(this); 
    mySQLiteAdapter.openToRead(); 

    Cursor cursor = mySQLiteAdapter.queueAll(); 
    startManagingCursor(cursor); 
    String[] from = new String[]{SQLiteAdapter.KEY_CONTENT}; 
    int[] to = new int[] {R.id.text}; 

    Log.v(LOG_TAG, "from value:" + from); 
    Log.v(LOG_TAG, "to value:" + to); 

    SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this, R.layout.row, cursor, from, to); 
    listContent.setAdapter(cursorAdapter); 

    mySQLiteAdapter.close(); 

} 
} 

我SQLiteAdapter:

package com.android.dbtest; 

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

public class SQLiteAdapter { 
public static final String MYDATABASE_NAME = "MY_DATABASE"; 
public static final String MYDATABASE_TABLE = "MY_TABLE"; 
public static final int MYDATABASE_VERSION = 1; 
public static final String KEY_CONTENT = "Content"; 
public static final String KEY_ID = "_id";   //primary key 


//create table MY_DATABASE (ID integer primary key, Content text not null); 
private static final String SCRIPT_CREATE_DATABASE = "create table " 
+ MYDATABASE_TABLE + " (" 
+ KEY_ID + " integer primary key autoincrement, "   
+ KEY_CONTENT + " text not null " + 
     ");"; 

private SQLiteHelper sqLiteHelper; 
private SQLiteDatabase sqLiteDatabase; 
private Context context; 
public SQLiteAdapter(Context c) 
{ 
    context = c; 
} 

public SQLiteAdapter openToRead() throws android.database.SQLException 
{ 
    sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION); 
    sqLiteDatabase = sqLiteHelper.getReadableDatabase(); 
    return this; 
} 
public SQLiteAdapter openToWrite() throws android.database.SQLException { 
    sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION); 
    sqLiteDatabase = sqLiteHelper.getWritableDatabase(); return this; 
    } 
public void close(){ 
    sqLiteHelper.close(); 
    } 
public long insert(String content){ 
    ContentValues contentValues = new ContentValues(); 
    contentValues.put(KEY_CONTENT, content); 
    return sqLiteDatabase.insert(MYDATABASE_TABLE, null, contentValues); 
    } 

public int deleteAll(){ 
    return sqLiteDatabase.delete(MYDATABASE_TABLE, null, null); 
    } 



public Cursor queueAll(){ 
    String[] columns = new String[]{KEY_ID, KEY_CONTENT}; 
    Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE, columns,  null, null, null, null, null); 
    return cursor; 
} 


public class SQLiteHelper extends SQLiteOpenHelper 
{ 
    public SQLiteHelper(Context context, String name, CursorFactory factory, int version) 
    { 
     super(context, name, factory, version); 
     } 
    @Override public void onCreate(SQLiteDatabase db) 
    { 
     // TODO Auto-generated method stub 
     db.execSQL(SCRIPT_CREATE_DATABASE); 
     } 
    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
    { 
     // TODO Auto-generated method stub } } } 
} 
} 
} 

終於爲顯示XML文件:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
    <TextView android:layout_height="wrap_content" 
    android:text="TextView" 
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:layout_width="wrap_content" 
    android:id="@+id/textView1"></TextView>  

    <ListView android:id="@android:id/android:list" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"/> 

</LinearLayout> 

回答

2

你不能這樣做就像在你的活動,如靜態初始化:

ListView listContent = (ListView)findViewById(R.id.contentlist); 

原因是你還沒有nto爲您的活動加載佈局,因此此通話將失敗findViewById(R.id.contentlist)。你的錯誤來源於類靜態初始化,因爲你不聲明你的類的構造函數,仍然得到錯誤的<init>.

修復:在onCreate初始化列表視圖中,你叫setContentView(R.layout.show);後。

+0

感謝 - 它釘住了移動ListView直到OnCreate()之後,我也對你的答案表示贊同。我感謝你們兩位! – CornishDibley 2012-03-14 17:07:02

+0

雖然我現在已經陷入另一個運行時錯誤:沒有這樣的列:_id:,而編譯:SELECT _id,內容從MY_TABLE - 應該讓我忙一會兒更長時間! – CornishDibley 2012-03-14 17:07:46

+0

在另一個問題中發佈更多信息!我喜歡數據庫錯誤!我最好的! – 2012-03-14 17:12:31

1

ShowActivity活動具有含ListView一個id @android:id/list一個佈局,但你搜索ListView控件與ID R.id.contentlist。在設置佈局後,您還必須在onCreate()方法中初始化您的視圖。