2014-01-20 94 views
0

我有問題從sqlite獲取我的行並將其顯示到我的textview。我得到的行ID很好,我相信,我用烤麪包,它出現了。但是,一旦我嘗試拉取該信息並顯示它,應用程序就會崩潰。從sqlite信息設置textview

日誌貓:

01-19 20:09:14.577: E/AndroidRuntime(1680): FATAL EXCEPTION: main 
01-19 20:09:14.577: E/AndroidRuntime(1680): Process: com.example.dba, PID: 1680 
01-19 20:09:14.577: E/AndroidRuntime(1680): java.lang.RuntimeException: Unable to start   activity ComponentInfo{com.example.dba/com.example.dba.ListInfo}:  java.lang.NullPointerException 
01-19 20:09:14.577: E/AndroidRuntime(1680): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) 
01-19 20:09:14.577: E/AndroidRuntime(1680): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 
01-19 20:09:14.577: E/AndroidRuntime(1680): at android.app.ActivityThread.access$800(ActivityThread.java:135) 
01-19 20:09:14.577: E/AndroidRuntime(1680): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
01-19 20:09:14.577: E/AndroidRuntime(1680): at android.os.Handler.dispatchMessage(Handler.java:102) 
01-19 20:09:14.577: E/AndroidRuntime(1680): at android.os.Looper.loop(Looper.java:136) 
01-19 20:09:14.577: E/AndroidRuntime(1680): at android.app.ActivityThread.main(ActivityThread.java:5017) 
01-19 20:09:14.577: E/AndroidRuntime(1680): at java.lang.reflect.Method.invokeNative(Native Method) 
01-19 20:09:14.577: E/AndroidRuntime(1680): at java.lang.reflect.Method.invoke(Method.java:515) 
01-19 20:09:14.577: E/AndroidRuntime(1680): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
01-19 20:09:14.577: E/AndroidRuntime(1680): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
01-19 20:09:14.577: E/AndroidRuntime(1680): at dalvik.system.NativeStart.main(Native Method) 
01-19 20:09:14.577: E/AndroidRuntime(1680): Caused by: java.lang.NullPointerException 
01-19 20:09:14.577: E/AndroidRuntime(1680): at  com.example.dba.ListInfo.populateListViewFromDB(ListInfo.java:78) 
01-19 20:09:14.577: E/AndroidRuntime(1680): at com.example.dba.ListInfo.onCreate(ListInfo.java:41) 
01-19 20:09:14.577: E/AndroidRuntime(1680): at android.app.Activity.performCreate(Activity.java:5231) 
01-19 20:09:14.577: E/AndroidRuntime(1680): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
01-19 20:09:14.577: E/AndroidRuntime(1680): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 

ListInfo.java

package com.example.dba; 

import com.example.dba.database.DBAdapter; 

import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.database.Cursor; 
import android.os.Bundle; 
import android.support.v4.widget.SimpleCursorAdapter; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.TextView; 
import android.widget.Toast; 

public class ListInfo extends Activity 
{ 
DBAdapter myDb; 
int ID_Number; 

    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.eventpage); 

     Context context = getApplicationContext(); 

     // GET OUR ID FROM WHERE THE EVENT THEY CLICKED 
     String ID = this.getIntent().getStringExtra("ROW_ID"); 

     // HOW LONG TO SHOW OUR TOAST MESSAGE 
     int duration = Toast.LENGTH_SHORT; 

     // SET UP THE TOAST 
     //Toast toast = Toast.makeText(context, ID, duration); 
     // SHOW THE MESSAGE 
     //toast.show(); 

     ID_Number = Integer.parseInt(ID); 

     populateListViewFromDB(ID_Number); 
    } 

    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; 
    } 

    public boolean onOptionsItemSelected(MenuItem item) 
    { 
     switch (item.getItemId()) 
     { 
      case R.id.action_event: 
       toggleEvent(); 
       break; 
      case R.id.action_places: 
       togglePlaces(); 
       break; 
     }  
      return super.onOptionsItemSelected(item); 
    } 

    private void togglePlaces() 
    {  
     startActivity(new Intent(this, MapPane.class));  
    } 

    private void toggleEvent() 
    {      
     startActivity(new Intent(this, Events.class));  
    } 

    @SuppressWarnings("deprecation") 
    private void populateListViewFromDB(int ID) 
    { 
     Cursor cursor = myDb.getRow(ID); 

     // Allow activity to manage lifetime of the cursor. 
     // DEPRECATED! Runs on the UI thread, OK for small/short queries. 
     startManagingCursor(cursor); 

     TextView iname = (TextView)findViewById(R.id.item_name); 
     iname.setText(DBAdapter.KEY_NAME); 

     TextView itime = (TextView)findViewById(R.id.item_time); 
     iname.setText(DBAdapter.KEY_TIME); 

     TextView iprice = (TextView)findViewById(R.id.item_price); 
     iname.setText(DBAdapter.KEY_PRICE); 

     TextView iloc = (TextView)findViewById(R.id.item_loc); 
     iname.setText(DBAdapter.KEY_LOC); 
    } 
} 

XML文件:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:screenOrientation="landscape"> 

<TextView 
    android:id="@+id/item_name" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:text="@string/header_event" 
    android:textAppearance="?android:attr/textAppearanceSmall" /> 

<TextView 
    android:id="@+id/item_time" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentRight="true" 
    android:layout_below="@+id/item_name" 
    android:text="@string/event_time" 
    android:textAppearance="?android:attr/textAppearanceSmall" /> 

<TextView 
    android:id="@+id/item_price" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentRight="true" 
    android:layout_below="@+id/item_time" 
    android:layout_marginRight="25dp" 
    android:text="@string/event_price" 
    android:textAppearance="?android:attr/textAppearanceSmall" /> 

<TextView 
    android:id="@+id/item_loc" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_below="@+id/item_price" 
    android:text="@string/event_loc" 
    android:textAppearance="?android:attr/textAppearanceSmall" /> 

</RelativeLayout> 

數據庫光標:

// Get a specific row (by rowId) 
public Cursor getRow(long rowId) 
{ 
    String where = KEY_ROWID + "=" + rowId; 
    Cursor c = db.query(true, DATABASE_TABLE, ALL_KEYS, 
        where, null, null, null, null, null); 
    if (c != null) 
    { 
     c.moveToFirst(); 
    } 
    return c; 
} 

回答

0

myDbDBAdapter的實例類爲null,因此在從DBAdapter類調用getRow方法之前進行初始化。

// initialize here... 
myDb=new DBAdapter(ListInfo.this); 
Cursor cursor = myDb.getRow(ID); 
+0

同樣的事情,只是崩潰。 – Jayce

0

在您的崩潰日誌的關鍵線路改爲:

產生的原因:顯示java.lang.NullPointerException 01-19 20:09:14.577:E/AndroidRuntime(1680年):在 COM .example.dba.ListInfo.populateListViewFromDB(ListInfo.java:78)

你似乎訪問在線78 myDb,它爲空。你有沒有實例化它?