我正在提取JSON数据并将其插入到我的SQLite数据库中。我已经在另一个类中编写了数据库插入代码,并且在doInBackground函数中调用了它的实例,但是这样做时我遇到了一些错误。我的AsyncTask代码如下。
package com.project.attendancemanager;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class LoginActivity extends Activity {
EditText username, password;
Button submit;
private ProgressDialog pDialog;
private static String KEY_SUCCESS="success";
private static String TABLES="tables";
private static String T_NAME="tname";
private static String T_DATA="tdata";
private static String TEACH_ID="TeachID";
private static String TEACH_NAME="TeachName";
private static String TEACH_USER="Username";
private static String TEACH_PASS="Password";
private static String CS_ID="CSID";
private static String CLASS_ID="ClassID";
private static String SUB_ID="SubID";
private static String CLASS_NAME="ClassName";
private static String COU_ID="CouID";
private static String COU_NAME="CouName";
private static String SUB_NAME="SubName";
private static String SEM_NAME="SemName";
private static String STUD_ID="StudID";
private static String STUD_NAME="StudName";
private static String STUD_USER="Username";
private static String STUD_PASS="Password";
DBHandler db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
username=(EditText)findViewById(R.id.loginET);
password=(EditText)findViewById(R.id.passwordET);
submit=(Button)findViewById(R.id.submitBT);
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
new LoginDetails().execute();
}
});
}
class LoginDetails extends AsyncTask<String, String, String>{
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
pDialog=new ProgressDialog(LoginActivity.this);
pDialog.setMessage("Retrieving Data...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
String user=username.getText().toString();
String pass=password.getText().toString();
System.out.println(user);
System.out.println(pass);
UserFunctions userFunctions=new UserFunctions();
JSONObject json=userFunctions.loginUser(user, pass);
try{
if(json.getString(KEY_SUCCESS)!=null){
String res=json.getString(KEY_SUCCESS);
if(Integer.parseInt(res)==1){
JSONArray tab=json.getJSONArray(TABLES);
//For TeacherTB data extraction
JSONObject TeacherTBJson=tab.getJSONObject(0);
String TeacherTB=TeacherTBJson.getString(T_NAME);
System.out.println(TeacherTB);
JSONArray tdata1=TeacherTBJson.getJSONArray(T_DATA);
JSONObject teachdata=tdata1.getJSONObject(0);
String teachID=teachdata.getString(TEACH_ID); //TeachID data extraction
System.out.println(teachID);
String teachName=teachdata.getString(TEACH_NAME); //TeachName data extraction
System.out.println(teachName);
String teachUser=teachdata.getString(TEACH_USER); //Username data extraction
System.out.println(teachUser);
String teachPass=teachdata.getString(TEACH_PASS); //Password data extraction
System.out.println(teachPass);
db.insertTeachTB(teachID, teachName, teachUser, teachPass);
//publishProgress(teachID, teachName, teachUser, teachPass);
//For ClassSubTB data extraction
JSONObject ClassSubTBJson=tab.getJSONObject(1);
String ClassSubTB=ClassSubTBJson.getString(T_NAME);
System.out.println(ClassSubTB);
JSONArray tdata2=ClassSubTBJson.getJSONArray(T_DATA);
System.out.println(tdata2.length());
JSONObject classSubid=tdata2.getJSONObject(0);
JSONArray classSubidArr=classSubid.getJSONArray(CS_ID);
JSONArray classIDArr=classSubid.getJSONArray(CLASS_ID);
JSONArray subIDArr=classSubid.getJSONArray(SUB_ID);
JSONArray teachidArr=classSubid.getJSONArray(TEACH_ID);
for(int i=0;i<classSubidArr.length();i++)
{
JSONArray classSubidArray=classSubidArr.getJSONArray(i);
JSONArray classidArray=classIDArr.getJSONArray(i);
JSONArray subidArray=subIDArr.getJSONArray(i);
JSONArray teachidArray=teachidArr.getJSONArray(i);
for(int j=0;j<classSubidArray.length();j++)
{
String csid=classSubidArray.getString(j);
String classid=classidArray.getString(j);
String subid=subidArray.getString(j);
String teachid=teachidArray.getString(j);
db.insertClassSubTB(csid, classid, subid, teachid);
}
}
}
else {
System.out.println("OOPS ERROR!");
}
}
}catch(JSONException e){
e.printStackTrace();
}
return null;
}
@Override
protected void onProgressUpdate(String... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
db.insertTeachTB(values[0], values[1], values[2], values[3]);
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
pDialog.dismiss();
}
}
}
db是我的DatabaseHandler类的实例,其中插入发生。
Logcat如下:
09-08 13:15:43.650: W/dalvikvm(1469): threadid=11: thread exiting with uncaught exception (group=0xb1a91ba8)
09-08 13:15:43.850: E/AndroidRuntime(1469): FATAL EXCEPTION: AsyncTask #1
09-08 13:15:43.850: E/AndroidRuntime(1469): Process: com.project.attendancemanager, PID: 1469
09-08 13:15:43.850: E/AndroidRuntime(1469): java.lang.RuntimeException: An error occured while executing doInBackground()
09-08 13:15:43.850: E/AndroidRuntime(1469): at android.os.AsyncTask$3.done(AsyncTask.java:300)
09-08 13:15:43.850: E/AndroidRuntime(1469): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
09-08 13:15:43.850: E/AndroidRuntime(1469): at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
09-08 13:15:43.850: E/AndroidRuntime(1469): at java.util.concurrent.FutureTask.run(FutureTask.java:242)
09-08 13:15:43.850: E/AndroidRuntime(1469): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
09-08 13:15:43.850: E/AndroidRuntime(1469): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
09-08 13:15:43.850: E/AndroidRuntime(1469): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
09-08 13:15:43.850: E/AndroidRuntime(1469): at java.lang.Thread.run(Thread.java:841)
09-08 13:15:43.850: E/AndroidRuntime(1469): Caused by: java.lang.NullPointerException
09-08 13:15:43.850: E/AndroidRuntime(1469): at com.project.attendancemanager.LoginActivity$LoginDetails.doInBackground(LoginActivity.java:110)
09-08 13:15:43.850: E/AndroidRuntime(1469): at com.project.attendancemanager.LoginActivity$LoginDetails.doInBackground(LoginActivity.java:1)
09-08 13:15:43.850: E/AndroidRuntime(1469): at android.os.AsyncTask$2.call(AsyncTask.java:288)
09-08 13:15:43.850: E/AndroidRuntime(1469): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
09-08 13:15:43.850: E/AndroidRuntime(1469): ... 4 more
09-08 13:15:46.860: I/Choreographer(1469): Skipped 42 frames! The application may be doing too much work on its main thread.
09-08 13:15:52.610: E/WindowManager(1469): android.view.WindowLeaked: Activity com.project.attendancemanager.LoginActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{b1f87078 V.E..... R......D 0,0-684,192} that was originally added here
09-08 13:15:52.610: E/WindowManager(1469): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:348)
09-08 13:15:52.610: E/WindowManager(1469): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:248)
09-08 13:15:52.610: E/WindowManager(1469): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
09-08 13:15:52.610: E/WindowManager(1469): at android.app.Dialog.show(Dialog.java:286)
09-08 13:15:52.610: E/WindowManager(1469): at com.project.attendancemanager.LoginActivity$LoginDetails.onPreExecute(LoginActivity.java:76)
09-08 13:15:52.610: E/WindowManager(1469): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587)
09-08 13:15:52.610: E/WindowManager(1469): at android.os.AsyncTask.execute(AsyncTask.java:535)
09-08 13:15:52.610: E/WindowManager(1469): at com.project.attendancemanager.LoginActivity$1.onClick(LoginActivity.java:61)
09-08 13:15:52.610: E/WindowManager(1469): at android.view.View.performClick(View.java:4438)
09-08 13:15:52.610: E/WindowManager(1469): at android.view.View$PerformClick.run(View.java:18422)
09-08 13:15:52.610: E/WindowManager(1469): at android.os.Handler.handleCallback(Handler.java:733)
09-08 13:15:52.610: E/WindowManager(1469): at android.os.Handler.dispatchMessage(Handler.java:95)
09-08 13:15:52.610: E/WindowManager(1469): at android.os.Looper.loop(Looper.java:136)
09-08 13:15:52.610: E/WindowManager(1469): at android.app.ActivityThread.main(ActivityThread.java:5017)
09-08 13:15:52.610: E/WindowManager(1469): at java.lang.reflect.Method.invokeNative(Native Method)
09-08 13:15:52.610: E/WindowManager(1469): at java.lang.reflect.Method.invoke(Method.java:515)
09-08 13:15:52.610: E/WindowManager(1469): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
09-08 13:15:52.610: E/WindowManager(1469): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
09-08 13:15:52.610: E/WindowManager(1469): at dalvik.system.NativeStart.main(Native Method)
我是这种Android编程的新手,所以任何帮助将不胜感激。谢谢!干杯!
答案 0 :(得分:0)
您收到此错误,因为在引用数据库处理程序对象(DBHnadler)db时,db未初始化。您正在对当前指向null的实例执行操作。
在执行插入之前,请执行以下操作:
db = new DBHandler;
db.getWritableDatabase().insert(your_arguments);