致命的例外:主,进程:com.example.joelg.clapp,PID:3677

时间:2017-10-18 09:30:42

标签: java android nullpointerexception android-sqlite

你好我正在android studio中构建我的第一个应用程序,经过一天试图让我的应用程序正常工作后,我遇到了以下错误,因为我是android工作室和编程的新手,总的来说,我想要一些帮助诊断问题 提前感谢您提供的任何帮助

10-18 09:21:45.827 3677-3677/com.example.joelg.clapp E/AndroidRuntime: FATAL EXCEPTION: main
                                                                   Process: com.example.joelg.clapp, PID: 3677
                                                                   java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.joelg.clapp/com.example.joelg.clapp.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
                                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2718)
                                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
                                                                       at android.app.ActivityThread.-wrap11(Unknown Source:0)
                                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
                                                                       at android.os.Handler.dispatchMessage(Handler.java:105)
                                                                       at android.os.Looper.loop(Looper.java:164)
                                                                       at android.app.ActivityThread.main(ActivityThread.java:6541)
                                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                                       at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
                                                                    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
                                                                       at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:118)
                                                                       at android.support.v7.app.AppCompatDelegateImplV9.<init>(AppCompatDelegateImplV9.java:152)
                                                                       at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:29)
                                                                       at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:53)
                                                                       at android.support.v7.app.AppCompatDelegateImplV23.<init>(AppCompatDelegateImplV23.java:31)
                                                                       at android.support.v7.app.AppCompatDelegateImplN.<init>(AppCompatDelegateImplN.java:31)
                                                                       at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:200)
                                                                       at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:184)
                                                                       at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:518)
                                                                       at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:189)
                                                                       at com.example.joelg.clapp.MainActivity.<init>(MainActivity.java:54)
                                                                       at java.lang.Class.newInstance(Native Method)
                                                                       at android.app.Instrumentation.newActivity(Instrumentation.java:1173)
                                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2708)
                                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) 
                                                                       at android.app.ActivityThread.-wrap11(Unknown Source:0) 
                                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) 
                                                                       at android.os.Handler.dispatchMessage(Handler.java:105) 
                                                                       at android.os.Looper.loop(Looper.java:164) 
                                                                       at android.app.ActivityThread.main(ActivityThread.java:6541) 
                                                                       at java.lang.reflect.Method.invoke(Native Method) 
                                                                       at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 

这是我的主要活动类

package com.example.joelg.clapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.security.Timestamp;
import java.security.cert.CertPath;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

DBHandler dbHandler;
ArrayAdapter<String> mAdapter;
ListView lstJob;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

 dbHandler = new DBHandler(this);

    lstJob = (ListView)findViewById(R.id.lstJob);

  LoadTaskList();

}
 private void LoadTaskList() {
     ArrayList<String> tasklist = dbHandler.getJobList();
     if(mAdapter==null){
         mAdapter = new ArrayAdapter<String>
(this,R.layout.row,R.id.Job_name,tasklist);
         lstJob.setAdapter(mAdapter);
     }else {
 mAdapter.clear();
         mAdapter.addAll(tasklist);
         mAdapter.notifyDataSetChanged();
         }
}


public void JobComplete(View view){
    View parent = (View)view.getParent();
}
    TextView taskTextView = (TextView)findViewById(R.id.Job_name);
}                                                         

DBHandler

package com.example.joelg.clapp;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by joelg on 6/10/2017.
 */

public class DBHandler extends SQLiteOpenHelper{
//database Version
private static final int DATABASE_VERSION = 1;
//database Name
private static final String DATABASE_NAME = "List";
//Contacts name table
private static final String DB_SHOPS="Shops";
//JobDB table columns names
private static final String DB_TABLE = "Job";
private static final String DB_COLUM ="JobNM";
private static final String DB_MOTEL_ADDR ="Motel_Address";
private static final String DB_PHOTO="Photo";

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


// creating tables
@Override
public void onCreate(SQLiteDatabase db){
    String query = String.format  ("CREATE DB %s (ID INTEGER PRIMARY KEY 
AUTOINCREMENT, %s TEXT NOT NULL);",DB_COLUM,DB_TABLE );
    db.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
     // drop older table if existed
    db.execSQL("DROP DB IF EXISTS" + DB_SHOPS);
    // create tables again
    onCreate(db);
}
// ADDING NEW Motel address

    public void addclean (CleanList cleanList){
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(DB_TABLE, cleanList.getRoomNum()); // Room number
    values.put(DB_COLUM, cleanList.getAddress()); // Motel address
    //inserting row
    db.insert(DB_SHOPS,null,values);
    db.close(); //close db connection





   }
   public CleanList cleanList (int id) {
   SQLiteDatabase db = this.getReadableDatabase();

   Cursor cursor = db.query(DB_SHOPS, new String[]{DB_TABLE, 
   DB_COLUM,DB_MOTEL_ADDR,}, DB_TABLE + "=?"
           , new String[]{String.valueOf(id)}, null, null, null, null);
   if (cursor != null)
       cursor.moveToFirst();

   CleanList contact = new CleanList(Integer.parseInt(cursor.getString(0)),
           cursor.getString(1), cursor.getString(2),cursor.getString(3));
   //return contact
   return contact;
   }
    // Getting All Job lists
      public   List<CleanList> getAllJobList() {
        List<CleanList> CleanList = new ArrayList<CleanList>();
    // Select All Query
        String selectQuery = "SELECT * FROM " + DB_SHOPS;

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

   // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                CleanList List = new CleanList();
                List.setId(Integer.parseInt(cursor.getString(0)));
                List.setRnum(cursor.getString(1));
                List.setAddress(cursor.getString(2));
                // Adding contact to list
                CleanList.add(List);
            } while (cursor.moveToNext());
        }
       // return count list

    return CleanList;
}
// Updating a Job
public int updateJob(CleanList cleanList) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(DB_COLUM, cleanList.getRoomNum());
    values.put(DB_TABLE, cleanList.getAddress());

// updating row
    return db.update(DB_SHOPS, values, DB_TABLE +  "= ?",
            new String[]{String.valueOf(cleanList.getId())});
}
// Deleting a JOB
public void deleteJob (CleanList cleanList) {
    SQLiteDatabase db = this.getWritableDatabase();
    db.delete(DB_SHOPS, DB_TABLE + " = ?",new String[] { 
String.valueOf(cleanList.getId()) });

}

// Getting shops Count
public ArrayList<String> getJobList() {
   ArrayList<String> taskList = new ArrayList<>();
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor =  db.query(DB_TABLE,new String[]
{DB_COLUM},null,null,null,null,null);
    while(cursor.moveToNext()){
        int index = cursor.getColumnIndex(DB_COLUM);
        taskList.add(cursor.getString(index));

    }
    cursor.close();
    db.close();
    return getJobList();
}
// return count



 } 

0 个答案:

没有答案