-3

これは、getbeat メソッドから getquery を呼び出す im のコードですが、「仮想メソッド 'android.database.Cursor android.database.sqlite.SQLiteDatabase.rawQuery(java.lang.String, java.lang. null オブジェクト参照の String[])'

public class report extends Fragment {
    private ImageButton BtnImgCal;
    private Spinner spinbt;
    private Spinner spindr;
    private Calendar cal;
    private FragmentActivity myContext;
    Button btnsun;
    int day;
    int month;
    int year;
    private EditText et;
    static final int DATE_PICKER_ID = 1111;
    private int Year;
    private int Month ;
    private int  Day ;
    String[] MonthNames = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
    GetLocation loc;
    double lati,longi;
    DBAdapter db;
    String[] beat;
    String[] Beat;
    public report(){}
                @Override
                public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) 
                { 
                     String[] strtext = getArguments().getStringArray("beatarray"); 
                     beat=strtext.clone();
                    View rootView = inflater.inflate(R.layout.call_report, container, false);         
                    return rootView;
                }

                 @Override
                 public void onViewCreated(final View view,Bundle savedInstanceState) 
                 {
                        super.onViewCreated(view, savedInstanceState);

                      db=new DBAdapter(getActivity());
                       GetBeat();

                  }

                        BtnImgCal = (ImageButton) getActivity().findViewById(R.id.ImgCalBtn);
                        cal = Calendar.getInstance();
                        day = cal.get(Calendar.DAY_OF_MONTH);
                        month = cal.get(Calendar.MONTH);
                        year = cal.get(Calendar.YEAR);
                        et = (EditText) getActivity().findViewById(R.id.etdate);
                        et.setText(day+"-"+ MonthNames[month] +"-"+year);
                        spinbt=(Spinner) this.getActivity().findViewById(R.id.spinBeat);
                        spinbt.setPrompt("Select Beat");

                    btnsun = (Button)this.getActivity().findViewById(R.id.btn_submit);              



                        final ToggleButton toggleButton = (ToggleButton) getActivity().findViewById(R.id.tglbtn);
                        toggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener() 


                        BtnImgCal.setOnClickListener(new View.OnClickListener() 
                        {
                            @Override
                            public void onClick(View view) 
                            {
                                //Toast.makeText(getActivity(),"Daily Call Summary",Toast.LENGTH_SHORT).show();
                                DialogFragment newFragment = new SelectDateFragment();
                                newFragment.show(getFragmentManager(), "DatePicker");
                            }

                        });
                 }

                 private void GetBeat() {

                        Cursor gb=db.getQueryResult("Select beatid,beat_name from beat_mas");
                        if(gb.getCount()>0)
                        {
                         Beat = new String[gb.getCount()+1]; 
                           int i=0; 
                           Beat[0]="Select Beat";
                        if (gb.moveToFirst())
                         {


                               if (gb.moveToFirst())                
                               {                
                                     do 
                                     { 
                                         Beat[i+1] =gb.getString(1) + " :ID-" + gb.getInt(0);  //Town Id
                                          i+=1; 

                                     } while (gb.moveToNext());                                                                   
                               }            
                               gb.close();   //close db      
                         }
            }
    }

私のDBAdapterは

public class DBAdapter 
{   
    public static final String KEY_ROWID = "_id";   //Table Fields ....
    private static final String TAG = "DBAdapter";    
    private static final String DATABASE_NAME = "bioderma";   //Database Name
    private static final int DATABASE_VERSION =41;
    public static String DateTime = null;


    private static final String DATABASE_CREATE_app_req =
        "create table app_req (_id integer primary key autoincrement, "
        + "req_no integer not null);";  

    //Login Master
    private static final String DATABASE_CREATE_login_mas =
            "create table login_mas (_id integer primary key autoincrement, userid integer not null, name text not null, "
            + "login_id text not null, password text not null, auth_key text not null, login_flag integer not null, "
            + "port_day integer null , port_month  integer null, port_year integer  null, " 
            + "user_type integer not null,sms_mode integer not null,dc_type integer not null);";
  //Beat Master
    private static final String DATABASE_CREATE_beat_mas =
            "create table beat_mas (_id integer primary key autoincrement, beatid integer not null,  "
            + "beat_name text not null);";

    //import log
    private static final String DATABASE_CREATE_import_log =            
            "create table import_log (_id integer primary key autoincrement, "
            + "port_flag integer not null);";  




    private final Context context; 

    private DatabaseHelper DBHelper;
    private SQLiteDatabase db;

    public DBAdapter(Context ctx)
    {
        this.context = ctx;
        System.out.println("this.context>>>>>>>>>>>>>>>>>"+ this.context);
        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_app_req);
            db.execSQL(DATABASE_CREATE_login_mas);
            db.execSQL(DATABASE_CREATE_beat_mas);
            db.execSQL(DATABASE_CREATE_import_log);

        }

        @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 app_req");
            db.execSQL("DROP TABLE IF EXISTS login_mas");   
            db.execSQL("DROP TABLE IF EXISTS beat_mas");
            db.execSQL("DROP TABLE IF EXISTS import_log");

            onCreate(db);           
        }
    } 

   //-------------------------- Insert ------------------------------------------------------------------------------



    public long insertBeat(int beatid, String beat_name)
    {
        ContentValues initialValues = new ContentValues();
        initialValues.put("beatid", beatid);
        initialValues.put("beat_name", beat_name);
        return db.insert("beat_mas", null, initialValues);  
    }

    public long insertimport(int imp_log) 
    {                   
        ContentValues initialValues = new ContentValues();
        initialValues.put("port_flag", imp_log);
        return db.insert("import_log", null, initialValues);    
    }



   //-------------------------- Insert Ends Here------------------------------------------------------------------------------



   //-------------------------- Update ------------------------------------------------------------------------------




    //executing query and it returns fields which are specified in the query ..........
    public Cursor getQueryResult(String MY_QUERY) throws SQLException 
    {  
        try {
            return db.rawQuery(MY_QUERY, null);    
        } catch (Exception e) {
            System.out.println("getQueryResult error>>>>>>>>>>>>> :" + e.toString());
        }
        return null;       

    }

}

フラグメントを使用している場合にのみ、この問題に直面しています...

注...アプリでHamburger_menuを使用しています....フラグメントA、B、C、DがありますすべてのフラグメントはMainActivityの子です..メインアクティビティではsqlite操作を実行できますが、フラグメントでは「試みnull オブジェクト参照で仮想メソッド「android.database.Cursor android.database.sqlite.SQLiteDatabase.rawQuery(java.lang.String, java.lang.String[])」を呼び出す」

ここに私のログがあります

02-03 12:56:43.482: I/art(17996): Debugger is active
02-03 12:56:43.672: I/System.out(17996): Debugger has connected
02-03 12:56:43.672: I/System.out(17996): waiting for debugger to settle...
02-03 12:56:43.872: I/System.out(17996): waiting for debugger to settle...
02-03 12:56:44.073: I/System.out(17996): waiting for debugger to settle...
02-03 12:56:44.276: I/System.out(17996): waiting for debugger to settle...
02-03 12:56:44.476: I/System.out(17996): waiting for debugger to settle...
02-03 12:56:44.676: I/System.out(17996): waiting for debugger to settle...
02-03 12:56:44.876: I/System.out(17996): waiting for debugger to settle...
02-03 12:56:45.077: I/System.out(17996): debugger has settled (1462)
02-03 12:56:45.123: V/Monotype(17996): SetAppTypeFace- try to flip, app = com.example.bioderma
02-03 12:56:45.131: V/Monotype(17996):     Typeface getFontPathFlipFont - systemFont = default#default
02-03 12:56:45.149: I/System.out(17996): this.context>>>>>>>>>>>>>>>>>com.example.bioderma.Login@3d712f6f
02-03 12:56:45.160: V/Monotype(17996): SetAppTypeFace- try to flip, app = com.example.bioderma
02-03 12:56:45.160: V/Monotype(17996):     Typeface getFontPathFlipFont - systemFont = default#default
02-03 12:56:48.119: I/System.out(17996): intent>>>>>>>>Intent { cmp=com.example.bioderma/.MainActivity }
02-03 12:56:48.140: I/System.out(17996): this.context>>>>>>>>>>>>>>>>>com.example.bioderma.MainActivity@181d2119
02-03 12:56:48.143: V/Monotype(17996): SetAppTypeFace- try to flip, app = com.example.bioderma
02-03 12:56:48.143: V/Monotype(17996):     Typeface getFontPathFlipFont - systemFont = default#default
02-03 12:56:48.876: I/System.out(17996): activity>>>>>>>>>>>>>>>>>android.app.Application@1a9dd12a
02-03 12:56:49.392: D/Network(17996): Network
02-03 12:56:49.392: I/System.out(17996): this.context>>>>>>>>>>>>>>>>>com.example.bioderma.MainActivity@181d2119
02-03 12:56:49.759: I/System.out(17996): getQueryResult error>>>>>>>>>>>>> :java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.Cursor android.database.sqlite.SQLiteDatabase.rawQuery(java.lang.String, java.lang.String[])' on a null object reference
02-03 12:56:55.398: D/AndroidRuntime(17996): Shutting down VM
02-03 12:56:55.402: E/AndroidRuntime(17996): FATAL EXCEPTION: main
02-03 12:56:55.402: E/AndroidRuntime(17996): Process: com.example.bioderma, PID: 17996
02-03 12:56:55.402: E/AndroidRuntime(17996): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bioderma/com.example.bioderma.MainActivity}: java.lang.NullPointerException: Attempt to invoke interface method 'int android.database.Cursor.getCount()' on a null object reference
02-03 12:56:55.402: E/AndroidRuntime(17996):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2316)
02-03 12:56:55.402: E/AndroidRuntime(17996):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2376)
02-03 12:56:55.402: E/AndroidRuntime(17996):    at android.app.ActivityThread.access$800(ActivityThread.java:147)
02-03 12:56:55.402: E/AndroidRuntime(17996):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281)
02-03 12:56:55.402: E/AndroidRuntime(17996):    at android.os.Handler.dispatchMessage(Handler.java:102)
02-03 12:56:55.402: E/AndroidRuntime(17996):    at android.os.Looper.loop(Looper.java:135)
02-03 12:56:55.402: E/AndroidRuntime(17996):    at android.app.ActivityThread.main(ActivityThread.java:5253)
02-03 12:56:55.402: E/AndroidRuntime(17996):    at java.lang.reflect.Method.invoke(Native Method)
02-03 12:56:55.402: E/AndroidRuntime(17996):    at java.lang.reflect.Method.invoke(Method.java:372)
02-03 12:56:55.402: E/AndroidRuntime(17996):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:900)
02-03 12:56:55.402: E/AndroidRuntime(17996):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:695)
02-03 12:56:55.402: E/AndroidRuntime(17996): Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'int android.database.Cursor.getCount()' on a null object reference
02-03 12:56:55.402: E/AndroidRuntime(17996):    at com.example.bioderma.report.GetBeat(report.java:171)
02-03 12:56:55.402: E/AndroidRuntime(17996):    at com.example.bioderma.report.onViewCreated(report.java:71)
02-03 12:56:55.402: E/AndroidRuntime(17996):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:908)
02-03 12:56:55.402: E/AndroidRuntime(17996):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
02-03 12:56:55.402: E/AndroidRuntime(17996):    at android.app.BackStackRecord.run(BackStackRecord.java:833)
02-03 12:56:55.402: E/AndroidRuntime(17996):    at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1452)
02-03 12:56:55.402: E/AndroidRuntime(17996):    at android.app.Activity.performStart(Activity.java:5990)
02-03 12:56:55.402: E/AndroidRuntime(17996):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2279)
02-03 12:56:55.402: E/AndroidRuntime(17996):    ... 10 more
02-03 12:56:57.830: I/Process(17996): Sending signal. PID: 17996 SIG: 9
4

1 に答える 1

0

次のチュートリアルをご覧ください。これが役立つ場合があります。

http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/

また、クラスdb内でクエリを実行しているときに変数を初期化します。DatabaseHelper

db = this.getWritableDatabase();
于 2016-02-03T07:43:58.117 に答える