我試圖通過按鈕的onClickListener啓動一個actvivity,並且總是我得到空指針異常。 我搜索並嘗試了很多不同的東西,但我找不到我犯了錯誤的地方。 下面是相關的代碼/班從StartActivity NullPointeException從按鈕
的崩潰活動:
public class DisplayData extends Activity{
TextView displayData;
DataObjectsProvider provider;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.displaydb);
provider = new DataObjectsProvider(this);
displayData = (TextView) findViewById(R.id.tvDisplayData);
displayData.setText(provider.getAlldobjects().toString());
}
}
(我試過讚揚了供應商和的setText代碼)
的onClickListener從工作主要活動:
bDisplay.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this, DisplayData.class);
startActivity(intent);
}
});
這裏是崩潰活動的佈局:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/tvDisplayData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="showData"
android:textAppearance="?android:attr/textAppearanceMedium" />
</ScrollView>
(我已經嘗試過,只用線性佈局的TextView)
,這裏是Android清單代碼:
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".DisplayData"
android:label="" >
</activity>
難道我做錯了什麼?有沒有我看不到的錯誤或者我錯過了什麼?
編輯: 日誌:
02-09 23:52:35.002:W/dalvikvm(24849):線程ID = 1:螺紋與未捕獲的異常(組= 0x40c971f8)離開 02- 09 23:52:35.052:E/AndroidRuntime(24849):致命例外:主 02-09 23:52:35.052:E/AndroidRuntime(24849):java.lang.RuntimeException:無法啓動活動ComponentInfo {com.d69 .dancesongsdb/com.d69.dancesongsdb.DisplayData}:java.lang.NullPointerException 02-09 23:52:35.052:E/AndroidRuntime(24849):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956) 02-09 23:52:35.052:E/AndroidRuntime(24849):at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981) 02-09 23:52:35.052:E/AndroidRuntime(24849):at android.app.ActivityThread.access $ 600(ActivityThread.java:123) 02-09 23:52:35.052:E/AndroidRuntime(24849):at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1147) 02-09 23:52:35.052:E/AndroidRuntime(24849):at android.os.Handler.dispatchMessage(Handler.java:99) 02-09 23:52:35.052:E/AndroidRuntime(24849):at android .os.Looper.loop(Looper.java:137) 02-09 23:52:35.052:E/AndroidRuntime(24849):at android.app.ActivityThread.main(ActivityThread.java:4424) 02-09 23 :52:35.052:E/AndroidRuntime(24849):at java.lang.reflect.Method.invokeNative(Native Method) 0 2-09 23:52:35.052:E/AndroidRuntime(24849):在java.lang.reflect.Method.invoke(Method.java:511) 02-09 23:52:35.052:E/AndroidRuntime(24849) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:787) 02-09 23:52:35.052:E/AndroidRuntime(24849):at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:554) 02-09 23:52:35.052:E/AndroidRuntime(24849):at dalvik.system.NativeStart.main(Native Method) 02-09 23:52:35.052:E/AndroidRuntime( 24849):導致:java.lang.NullPointerException 02-09 23:52:35.052:E/AndroidRuntime(24849):at com.d69.dancesongsdb.DataObjectsProvider.getAlldobjects(DataObjectsProvider.java:58) 02-09 23 :52:35.052:E/AndroidRuntime(24849):在com.d69.dancesongsdb.DisplayData.onCreate(DisplayData。java:19) 02-09 23:52:35.052:E/AndroidRuntime(24849):at android.app.Activity.performCreate(Activity.java:4465) 02-09 23:52:35.052:E/AndroidRuntime 24849):at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 02-09 23:52:35.052:E/AndroidRuntime(24849):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920) 02-09 23:52:35.052:E/AndroidRuntime(24849):...... 11多個
EDIT2 我已經放棄了的db4o和SQLite的重建我的課吧。但是這個錯誤仍然發生在活動上。我也編輯了崩潰活動的代碼。 最新一行(使用提供程序)是導致崩潰的一行。我已經通過改變與下面的代碼啓動活動按鈕的代碼測試,它的主要活動:
public void onClick(View v) {
Toast.makeText(MainActivity.this, provider.getAlldobjects().toString(), Toast.LENGTH_LONG).show();
和數據庫的項目顯示吐司正確。 關於主要活動(從上面的代碼)我已經聲明瞭DataObjectsProvider類與崩潰活動完全相同的方式。我無法理解爲什麼一個活動有效,而不是另一個。
DataObjectsProvider:
public class DataObjectsProvider {
// Database fields
private SQLiteDatabase database;
private DbHelper dbHelper;
private String[] allColumns = { DbHelper.COLUMN_ID,
DbHelper.COLUMN_SONGNAME, DbHelper.COLUMN_SONGARTIST, DbHelper.COLUMN_SONGGENRE };
public DataObjectsProvider (Context context) {
dbHelper = new DbHelper(context);
}
public void open() throws SQLException {
database = dbHelper.getWritableDatabase();
}
public void close() {
dbHelper.close();
}
public DataObjects createDataObjects (String songName, String songArtist, String songGenre){
ContentValues values = new ContentValues();
values.put(DbHelper.COLUMN_SONGNAME, songName + ", ");
values.put(DbHelper.COLUMN_SONGARTIST, songArtist + ", ");
values.put(DbHelper.COLUMN_SONGGENRE, songGenre);
long insertId = database.insert(DbHelper.TABLE_SONGS, null,
values);
Cursor cursor = database.query(DbHelper.TABLE_SONGS,
allColumns, DbHelper.COLUMN_ID + " = " + insertId, null,
null, null, null);
cursor.moveToFirst();
DataObjects newDobject = cursorToDataObjects(cursor);
cursor.close();
return newDobject;
}
public void deleteDobjects(DataObjects dobjects) {
long id = dobjects.getId();
System.out.println("Comment deleted with id: " + id);
database.delete(DbHelper.TABLE_SONGS, DbHelper.COLUMN_ID
+ " = " + id, null);
}
public List<DataObjects> getAlldobjects() {
List<DataObjects> dobjects = new ArrayList<DataObjects>();
Cursor cursor = database.query(DbHelper.TABLE_SONGS,
allColumns, null, null, null, null, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
DataObjects dobject = cursorToDataObjects(cursor);
dobjects.add(dobject);
cursor.moveToNext();
}
// Make sure to close the cursor
cursor.close();
return dobjects;
}
private DataObjects cursorToDataObjects(Cursor cursor) {
DataObjects dobject = new DataObjects();
dobject.setId(cursor.getLong(0));
dobject.setSongName(cursor.getString(1));
dobject.setSongArtist(cursor.getString(2));
dobject.setSongGenre(cursor.getString(3));
return dobject;
}
}
請告訴我的NullPointerException異常?你可以顯示錯誤信息 – jelgh 2013-02-09 14:32:31
Post logcat stacktrace。 – Kiril 2013-02-09 14:33:06
你沒有勾住你的按鈕'bDisplay' ... – jenzz 2013-02-09 14:34:37