2

ログ:

12-25 03:26:16.003: E/AndroidRuntime(3623): FATAL EXCEPTION: main
12-25 03:26:16.003: E/AndroidRuntime(3623): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dd.gfit/com.dd.gfit.DaysActivity}: java.lang.NullPointerException
12-25 03:26:16.003: E/AndroidRuntime(3623):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
12-25 03:26:16.003: E/AndroidRuntime(3623):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
12-25 03:26:16.003: E/AndroidRuntime(3623):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-25 03:26:16.003: E/AndroidRuntime(3623):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
12-25 03:26:16.003: E/AndroidRuntime(3623):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-25 03:26:16.003: E/AndroidRuntime(3623):     at android.os.Looper.loop(Looper.java:137)
12-25 03:26:16.003: E/AndroidRuntime(3623):     at android.app.ActivityThread.main(ActivityThread.java:5039)
12-25 03:26:16.003: E/AndroidRuntime(3623):     at java.lang.reflect.Method.invokeNative(Native Method)
12-25 03:26:16.003: E/AndroidRuntime(3623):     at java.lang.reflect.Method.invoke(Method.java:511)
12-25 03:26:16.003: E/AndroidRuntime(3623):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-25 03:26:16.003: E/AndroidRuntime(3623):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-25 03:26:16.003: E/AndroidRuntime(3623):     at dalvik.system.NativeStart.main(Native Method)
12-25 03:26:16.003: E/AndroidRuntime(3623): Caused by: java.lang.NullPointerException
12-25 03:26:16.003: E/AndroidRuntime(3623):     at com.dd.gfit.DaysActivity.onCreate(DaysActivity.java:22)
12-25 03:26:16.003: E/AndroidRuntime(3623):     at android.app.Activity.performCreate(Activity.java:5104)
12-25 03:26:16.003: E/AndroidRuntime(3623):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
12-25 03:26:16.003: E/AndroidRuntime(3623):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
12-25 03:26:16.003: E/AndroidRuntime(3623):     ... 11 more

以下に関連するコードを追加しました。

MySQLiteHelper.java:

package com.dd.gfit;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class MySQLiteHelper extends SQLiteOpenHelper {

  public static final String TABLE_DAYS = "days";

  public static final String COLUMN_ID = "id";
  public static final String COLUMN_NAME = "name";
  public static final String COLUMN_ID_ROUTINE = "id_routine";
  public static final String COLUMN_DAY = "day";


  private static final String DATABASE_NAME = "gfit.db";
  private static final int DATABASE_VERSION = 1;

  public MySQLiteHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
  }

  @Override
  public void onCreate(SQLiteDatabase database) {
        // create days table
        database.execSQL("create table " + TABLE_DAYS + "(" 
                + COLUMN_ID + " integer primary key autoincrement," 
                + COLUMN_ID_ROUTINE + " integer not null," 
                + COLUMN_NAME + " varchar not null," 
                + COLUMN_DAY + " varchar not null"
                + ")");
        database.execSQL("insert into " + TABLE_DAYS + "("+ COLUMN_ID +", "+ COLUMN_ID_ROUTINE +", "+ COLUMN_NAME +", "+ COLUMN_DAY +") VALUES (12, 23, 'Day 1', 'Monday')");
        database.execSQL("insert into " + TABLE_DAYS + "("+ COLUMN_ID +", "+ COLUMN_ID_ROUTINE +", "+ COLUMN_NAME +", "+ COLUMN_DAY +") VALUES (21, 23, 'Day 2', 'Tuesday')");
        database.execSQL("insert into " + TABLE_DAYS + "("+ COLUMN_ID +", "+ COLUMN_ID_ROUTINE +", "+ COLUMN_NAME +", "+ COLUMN_DAY +") VALUES (33, 23, 'Day 3', 'No Specific Day')");
  }

  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_DAYS);
    onCreate(db);
  }

} 

DaysActivity.java:

package com.dd.gfit;

import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.widget.SimpleCursorAdapter;

public class DaysActivity extends ListActivity {
  private DaysDataSource datasource;
  private long routineDataID;
  private SimpleCursorAdapter dataAdapter;

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

    routineDataID = getIntent().getLongExtra("routineDataID", 1);

      Cursor cursor = datasource.fetchAllDays(routineDataID);

      // The desired columns to be bound
      String[] columns = new String[] { MySQLiteHelper.COLUMN_NAME, MySQLiteHelper.COLUMN_DAY };

      // the XML defined views which the data will be bound to
      int[] to = new int[] { R.id.listitem_day_name, R.id.listitem_day_day };

      // create the adapter using the cursor pointing to the desired data 
      dataAdapter = new SimpleCursorAdapter(this, R.layout.listitem_day, cursor, columns, to, 0);

      // Assign adapter to ListView
      setListAdapter(dataAdapter);
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      MenuInflater inflater = getMenuInflater();
      inflater.inflate(R.menu.activity_days, menu);
      return true;
  }

} 

DaysDataSource.java:

package com.dd.gfit;

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

public class DaysDataSource {

  // Database fields
  private SQLiteDatabase database;
  private MySQLiteHelper dbHelper;
  private String[] allColumns = { MySQLiteHelper.COLUMN_ID, MySQLiteHelper.COLUMN_ID_ROUTINE, MySQLiteHelper.COLUMN_NAME, MySQLiteHelper.COLUMN_DAY };

  public DaysDataSource(Context context) {
    dbHelper = new MySQLiteHelper(context);
  }

  public void open() throws SQLException {
    database = dbHelper.getWritableDatabase();
  }

  public void close() {
    dbHelper.close();
  }

  public Cursor fetchAllDays(long id) {   
      Cursor mCursor = database.query(MySQLiteHelper.TABLE_DAYS, allColumns, null, null, null, MySQLiteHelper.COLUMN_ID_ROUTINE + " = " + id, null);

      if (mCursor != null) {
          mCursor.moveToFirst();
      }

      return mCursor;
  }
} 

listitem_day.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_vertical"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/listitem_day_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Day Name" />

    <TextView
        android:id="@+id/listitem_day_day"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp"
        android:text="Monday"
        android:textAppearance="?android:attr/textAppearanceSmall" />

</LinearLayout>

DaysActivityのインテントの呼び方:

Toast toast = Toast.makeText(this, String.valueOf(id), Toast.LENGTH_SHORT);
toast.show(); 

Intent startDaysActivity = new Intent(this, DaysActivity.class);
startDaysActivity.putExtra("routineDataID", id);
this.startActivity(startDaysActivity);

が正しく表示routineDataIDされているので、正常に渡されていることがわかります。Toastid

NullPointerExceptionが発生するのはなぜですか?

4

2 に答える 2

3

コードを次のように変更します。

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

    routineDataID = getIntent().getLongExtra("routineDataID", 1);

    datasource=new DaysDataSource(DaysActivity.this); //<<missing this line

    datasource.open() ;  //<<< Open db here       

    Cursor cursor = datasource.fetchAllDays(routineDataID);
     // your code here...

コンテキストオブアクティビティを渡してデータソースを初期化していない

于 2012-12-25T03:38:08.653 に答える
0
   Cursor cursor = datasource.fetchAllDays(routineDataID); 

データソースがnull

于 2012-12-25T03:39:48.003 に答える