2017-03-19 10 views
-3

concreteInボタンをクリックするとアプリがクラッシュする問題が発生しています。getWritableDatabaseがNullPointerExceptionをスローする

下記のコードはMaterialsInクラスからのものです。

package com.example.android.gatekeeper; 

import android.content.Intent; 
import android.os.Bundle; 
import android.provider.MediaStore; 
import android.support.v7.app.AppCompatActivity; 
import android.view.View; 
import android.widget.Button; 

import com.example.android.gatekeeper.gateData.DbAccess; 

import static com.example.android.gatekeeper.MaterialsOut.REQUEST_IMAGE_CAPTURE; 


public class MaterialsIn extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_materials_in); 

     // Find the (@Concrete) button. 
     Button concreteIn = (Button) findViewById(R.id.concrete_in); 

     // Set a click listener on that View 
     concreteIn.setOnClickListener(new View.OnClickListener() { 
      // The code in this method will be executed when the Concrete In button is clicked on. 
      @Override 
      public void onClick(View view) { 
       DbAccess db = new DbAccess(); 
       db.insertConcreteIn(); 
       Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
       if (takePicture.resolveActivity(getPackageManager()) != null) { 
        startActivityForResult(takePicture, REQUEST_IMAGE_CAPTURE); 
       } 
      } 
     }); 
    } 
} 

insertConcreteIn()メソッドが呼び出されると、アプリケーションがgetWritableDatabase()でクラッシュ。

package com.example.android.gatekeeper.gateData; 

import android.content.ContentValues; 
import android.database.sqlite.SQLiteDatabase; 
import android.support.v7.app.AppCompatActivity; 

import com.example.android.gatekeeper.gateData.Contract.GateEntry; 


public class DbAccess extends AppCompatActivity { 

    // To access our database, we instantiate our subclass of SQLiteOpenHelper 
    // and pass the context, which is the current activity. 
    DbHelper mDbHelper = new DbHelper(this); 

    // Insert data corresponding to the concrete in to the database. 
    public void insertConcreteIn() { 

     // Gets the data repository in write mode. 
     SQLiteDatabase SQLiteDb = mDbHelper.getWritableDatabase(); 

     // Create a new map of values, where column names are the keys. 
     ContentValues values = new ContentValues(); 
     values.put(GateEntry.COLUMN_DIRECTION, "To Site"); 
     values.put(GateEntry.COLUMN_TYPE, "Concrete"); 
     values.put(GateEntry.COLUMN_DATE, 170303); // Placeholder int for now. 
     values.put(GateEntry.COLUMN_TIME, 171930); // Placeholder int for now. 

     // Insert the new row, returning the primary key value of the new row. 
     long newRowId = SQLiteDb.insert(GateEntry.TABLE_NAME, null, values); 
    } 
} 

logcatは次のとおりです。

03-19 19:58:08.190 24751-24751/com.example.android.gatekeeper E/AndroidRuntime: FATAL EXCEPTION: main 
                       Process: com.example.android.gatekeeper, PID: 24751 
                       java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.sqlite.SQLiteDatabase android.content.Context.openOrCreateDatabase(java.lang.String, int, android.database.sqlite.SQLiteDatabase$CursorFactory, android.database.DatabaseErrorHandler)' on a null object reference 
                        at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:283) 
                        at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:223) 
                        at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163) 
                        at com.example.android.gatekeeper.gateData.DbAccess.insertConcreteIn(DbAccess.java:20) 
                        at com.example.android.gatekeeper.MaterialsIn$1.onClick(MaterialsIn.java:34) 
                        at android.view.View.performClick(View.java:5702) 
                        at android.widget.TextView.performClick(TextView.java:10888) 
                        at android.view.View$PerformClick.run(View.java:22541) 
                        at android.os.Handler.handleCallback(Handler.java:739) 
                        at android.os.Handler.dispatchMessage(Handler.java:95) 
                        at android.os.Looper.loop(Looper.java:158) 
                        at android.app.ActivityThread.main(ActivityThread.java:7229) 
                        at java.lang.reflect.Method.invoke(Native Method) 
                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 
03-19 19:58:08.190 3464-5216/? W/ActivityManager: Force finishing activity com.example.android.gatekeeper/.MaterialsIn 

ここにご協力ください。

+3

コードにバグがあります。あなたはそれを修正する必要があります。ヘルプが必要な場合は – ajb

+1

コードを入力してください。また、良い質問をする方法に関する文書を読むことを検討してください。 –

+0

あなたのコードは、Androidでインテント/アクティビティがどのように機能するか理解できないことを示しています。ここから始めるhttps://developer.android.com/training/index.html –

答えて

1

無効なContextSQLiteOpenHelperコンストラクタに渡しているという問題があります。

DbAccess db = new DbAccess(); 

public class DbAccess extends AppCompatActivity 

は決して Activityクラス自身をインスタンス化しないためのものです。このようなインスタンスは、 Contextとして使用するなど、アクティビティが必要なものには適していません。

フレームワークによって設定された有効なContextを、必要なメソッドに渡します。

+0

2番目の問題は 'new DbHelper(this)'です。 (ヒント:あなたの活動名は 'Activity'で終わるべきです、あなたはこの混乱に追随することはできません。)アクティビティはコンストラクタで完全に設定されていません。アクティビティ(またはコンテキスト)をパラメータとして受け取るすべてのインスタンス化は、 'onCreate'より早く行われます。 –

+1

@EugenPechanec一般的にはyesですが、 'SQLiteOpenHelper'の場合、' Context'は 'get {Read、Writ} ableDatabase()'(コンストラクタではありません)の時にのみ使用されます – laalto

+0

@IaaIto私は 'Context'の使い方を正しく理解していませんが、私はかなり新しいプログラミングです。この場合、有効な 'Context'をどのように渡しますか? 'MaterialsIn'から' DbAccess'をインスタンス化できない場合、 'insertConcreteIn()'メソッドをどのように呼び出すことができますか? –

関連する問題