DBAdapter不使用选项卡

时间:2012-01-09 17:02:11

标签: android database tabs

  

可能重复:
  Why do I get a null pointer exception from TabWidget?

我根据developer.android.com /.

的教程制作了标签视图

现在我已经从http://www.devx.com/wireless/Article/40842/0/page/1学习了数据库使用教程。

当我尝试结合使用DBAdapter和标签视图时,我最终会出现错误

Uncaught handler: thread main exiting due to uncaught exception. 

我对android编程很新,所以任何建议都值得赞赏。我已经尝试过搜索多个线程了。

01-09 19:52:50.941: D/ddm-heap(228): Got feature list request
01-09 19:52:51.590: D/AndroidRuntime(228): Shutting down VM
01-09 19:52:51.590: W/dalvikvm(228): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
01-09 19:52:51.590: E/AndroidRuntime(228): Uncaught handler: thread main exiting due to uncaught exception
01-09 19:52:51.630: E/AndroidRuntime(228): java.lang.NullPointerException
01-09 19:52:51.630: E/AndroidRuntime(228):  at android.widget.TabWidget.dispatchDraw(TabWidget.java:206)
01-09 19:52:51.630: E/AndroidRuntime(228):  at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
01-09 19:52:51.630: E/AndroidRuntime(228):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
01-09 19:52:51.630: E/AndroidRuntime(228):  at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
01-09 19:52:51.630: E/AndroidRuntime(228):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
01-09 19:52:51.630: E/AndroidRuntime(228):  at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
01-09 19:52:51.630: E/AndroidRuntime(228):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
01-09 19:52:51.630: E/AndroidRuntime(228):  at android.view.View.draw(View.java:6538)
01-09 19:52:51.630: E/AndroidRuntime(228):  at android.widget.FrameLayout.draw(FrameLayout.java:352)
01-09 19:52:51.630: E/AndroidRuntime(228):  at android.view.ViewGroup.drawChild(ViewGroup.java:1531)
01-09 19:52:51.630: E/AndroidRuntime(228):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
01-09 19:52:51.630: E/AndroidRuntime(228):  at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
01-09 19:52:51.630: E/AndroidRuntime(228):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
01-09 19:52:51.630: E/AndroidRuntime(228):  at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
01-09 19:52:51.630: E/AndroidRuntime(228):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
01-09 19:52:51.630: E/AndroidRuntime(228):  at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
01-09 19:52:51.630: E/AndroidRuntime(228):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
01-09 19:52:51.630: E/AndroidRuntime(228):  at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
01-09 19:52:51.630: E/AndroidRuntime(228):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
01-09 19:52:51.630: E/AndroidRuntime(228):  at android.view.View.draw(View.java:6538)
01-09 19:52:51.630: E/AndroidRuntime(228):  at android.widget.FrameLayout.draw(FrameLayout.java:352)
01-09 19:52:51.630: E/AndroidRuntime(228):  at android.view.ViewGroup.drawChild(ViewGroup.java:1531)
01-09 19:52:51.630: E/AndroidRuntime(228):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
01-09 19:52:51.630: E/AndroidRuntime(228):  at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
01-09 19:52:51.630: E/AndroidRuntime(228):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
01-09 19:52:51.630: E/AndroidRuntime(228):  at android.view.View.draw(View.java:6538)
01-09 19:52:51.630: E/AndroidRuntime(228):  at android.widget.FrameLayout.draw(FrameLayout.java:352)
01-09 19:52:51.630: E/AndroidRuntime(228):  at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1830)
01-09 19:52:51.630: E/AndroidRuntime(228):  at android.view.ViewRoot.draw(ViewRoot.java:1349)
01-09 19:52:51.630: E/AndroidRuntime(228):  at android.view.ViewRoot.performTraversals(ViewRoot.java:1114)
01-09 19:52:51.630: E/AndroidRuntime(228):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1633)
01-09 19:52:51.630: E/AndroidRuntime(228):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-09 19:52:51.630: E/AndroidRuntime(228):  at android.os.Looper.loop(Looper.java:123)
01-09 19:52:51.630: E/AndroidRuntime(228):  at android.app.ActivityThread.main(ActivityThread.java:4363)
01-09 19:52:51.630: E/AndroidRuntime(228):  at java.lang.reflect.Method.invokeNative(Native Method)
01-09 19:52:51.630: E/AndroidRuntime(228):  at java.lang.reflect.Method.invoke(Method.java:521)
01-09 19:52:51.630: E/AndroidRuntime(228):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
01-09 19:52:51.630: E/AndroidRuntime(228):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
01-09 19:52:51.630: E/AndroidRuntime(228):  at dalvik.system.NativeStart.main(Native Method)
01-09 19:52:51.670: I/dalvikvm(228): threadid=7: reacting to signal 3
01-09 19:52:51.670: E/dalvikvm(228): Unable to open stack trace file '/data/anr/traces.txt': Permission denied

这是我的活动,包括三个标签。

import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.TabHost;


public class Manager2012Activity extends TabActivity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Resources res = getResources(); // Resource object to get Drawables
        TabHost tabHost = getTabHost();  // The activity TabHost
        TabHost.TabSpec spec;  // Resusable TabSpec for each tab
        Intent intent;  // Reusable Intent for each tab

        // Create an Intent to launch an Activity for the tab (to be reused)
        intent = new Intent().setClass(this, TeamActivity.class);

        // Initialize a TabSpec for each tab and add it to the TabHost
        spec = tabHost.newTabSpec("team").setIndicator("team",
                          res.getDrawable(R.drawable.ic_tab_team))
                      .setContent(intent);
        tabHost.addTab(spec);

        // Do the same for the other tabs
        intent = new Intent().setClass(this, leagueActivity.class);
        spec = tabHost.newTabSpec("league").setIndicator("league",
                          res.getDrawable(R.drawable.ic_tab_league))
                      .setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, ContinueActivity.class);
        spec = tabHost.newTabSpec("continue").setIndicator("continue",
                          res.getDrawable(R.drawable.ic_tab_continue))
                      .setContent(intent);
        tabHost.addTab(spec);

        tabHost.setCurrentTab(2);

    }


}

这是DBAdapter。

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBAdapter 
{
        public static final String KEY_ROWID = "_id";
        public static final String KEY_DAY = "päivä";
        public static final String KEY_MONTH = "kuukausi";
        public static final String KEY_YEAR = "vuosi";
        private static final String TAG = "DBAdapter";

        private static final String DATABASE_NAME = "manager";
        private static final String DATABASE_TABLE = "game_date";
        private static final int DATABASE_VERSION = 1;

        private static final String DATABASE_CREATE =
            "create table game_date (_id integer primary key autoincrement, "
            + "päivä text not null, kuukausi text not null, " 
            + "vuosi text not null);";

        private final Context context; 

        private DatabaseHelper DBHelper;
        private SQLiteDatabase db;

        public DBAdapter(Context ctx) 
        {
            this.context = ctx;
            DBHelper = new DatabaseHelper(context);
        }

        private static class DatabaseHelper extends SQLiteOpenHelper 
        {
            DatabaseHelper(Context context) 
            {
                super(context, DATABASE_NAME, null, DATABASE_VERSION);
            }

            @Override
            public void onCreate(SQLiteDatabase db) 
            {
                db.execSQL(DATABASE_CREATE);
            }

            @Override
            public void onUpgrade(SQLiteDatabase db, int oldVersion, 
                                  int newVersion) 
            {
                Log.w(TAG, "Upgrading database from version " + oldVersion 
                      + " to "
                      + newVersion + ", which will destroy all old data");
                db.execSQL("DROP TABLE IF EXISTS game_date");
                onCreate(db);
            }
        }

        //---opens the database---
        public DBAdapter open() throws SQLException 
        {
            db = DBHelper.getWritableDatabase();
            return this;
        }

        //---closes the database---    
        public void close() 
        {
            DBHelper.close();
        }

        //---insert a title into the database---
        public long insertTitle(String päivä, String kuukausi, String vuosi) 
        {
            ContentValues initialValues = new ContentValues();
            initialValues.put(KEY_DAY, päivä);
            initialValues.put(KEY_MONTH, kuukausi);
            initialValues.put(KEY_YEAR, vuosi);
            return db.insert(DATABASE_TABLE, null, initialValues);
        }

        //---deletes a particular title---
        public boolean deleteTitle(long rowId) 
        {
            return db.delete(DATABASE_TABLE, KEY_ROWID + 
                    "=" + rowId, null) > 0;
        }

        //---retrieves all the titles---
        public Cursor getAllTitles() 
        {
            return db.query(DATABASE_TABLE, new String[] {
                    KEY_ROWID, 
                    KEY_DAY,
                    KEY_MONTH,
                    KEY_YEAR}, 
                    null, 
                    null, 
                    null, 
                    null, 
                    null);
        }

        //---retrieves a particular title---
        public Cursor getTitle(long rowId) throws SQLException 
        {
            Cursor mCursor =
                    db.query(true, DATABASE_TABLE, new String[] {
                            KEY_ROWID,
                            KEY_DAY, 
                            KEY_MONTH,
                            KEY_YEAR
                            }, 
                            KEY_ROWID + "=" + rowId, 
                            null,
                            null, 
                            null, 
                            null, 
                            null);
            if (mCursor != null) {
                mCursor.moveToFirst();
            }
            return mCursor;
        }

        //---updates a title---
        public boolean updateTitle(long rowId, String päivä, 
        String kuukausi, String vuosi) 
        {
            ContentValues args = new ContentValues();
            args.put(KEY_DAY, päivä);
            args.put(KEY_MONTH, kuukausi);
            args.put(KEY_YEAR, vuosi);
            return db.update(DATABASE_TABLE, args, 
                             KEY_ROWID + "=" + rowId, null) > 0;
        }



}

这是在数据库中插入数据并检索它的选项卡。

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.Toast;

public class ContinueActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

       DBAdapter db = new DBAdapter(this);

         db.open();        
         long id;
         id = db.insertTitle(
                "1",
                "1",
                "2012");    
         id = db.insertTitle(
                "15",
                "2",
                "2013");
         db.close();

         db.open();

        Cursor c = db.getTitle(2);
        if (c.moveToFirst())        
        DisplayTitle(c);
        else
            Toast.makeText(this, "No title found", 
        Toast.LENGTH_LONG).show();
        db.close();
    }

    public void DisplayTitle(Cursor c)
    {
        Toast.makeText(this, 
                "id: " + c.getString(0) + "\n" +
                "DAY: " + c.getString(1) + "\n" +
                "MONTH: " + c.getString(2) + "\n" +
        "YEAR:  " + c.getString(3),
        Toast.LENGTH_LONG).show();
     }  

}

1 个答案:

答案 0 :(得分:0)

感谢您的提示thinksteep!

我设法通过编辑标签的布局xml文件来实现它。