从sqlite信息设置textview

时间:2014-01-20 01:21:06

标签: android sqlite textview

我在从sqlite获取行并将其显示在textview中时遇到问题。我觉得行ID很好,我相信,我用吐司就出现了。但是,一旦我尝试提取信息并显示它,应用程序就会崩溃。

log cat:

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

2 个答案:

答案 0 :(得分:0)

myDb DBAdapter类的实例为null,因此在从DBAdapter类调用getRow方法之前进行初始化。

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

答案 1 :(得分:0)

崩溃日志中的关键行显示为:

  

引起:java.lang.NullPointerException   01-19 20:09:14.577:E / AndroidRuntime(1680):at   com.example.dba.ListInfo.populateListViewFromDB(ListInfo.java:78)

您似乎在第78行访问myDb并且它为空。你实例化了吗?

相关问题