我有数据到json arraylist当我试图使用green dao将数据存储到数据库时出错了。 我不能认识到出了什么问题
我的活动课程如下
package com.example.vollyexample;
import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONObject;
import com.android.volley.Request;
import com.android.volley.Request.Method;
import com.android.volley.Response;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.example.dao.Details;
import com.example.dao.DetailsDao;
import android.app.Activity;
import android.os.Bundle;
import android.os.IBinder.DeathRecipient;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity {
DetailsDao detailsDao;
Details details = new Details();
Button bt_display;
ListView lv;
Vollyapp vapp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
vapp=(Vollyapp) Vollyapp.getContext();
detailsDao = Vollyapp.dsession.getDetailsDao();
bt_display = (Button) findViewById(R.id.display_button);
lv = (ListView) findViewById(R.id.listView);
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
String item=String.valueOf(lv.getItemIdAtPosition(position));
Toast.makeText(getApplicationContext(),""+item,Toast.LENGTH_LONG).show();
}
});
bt_display.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
getdata();
}
});
String url = "http://api.androidhive.info/volley/person_array.json";
// Request a string response
JsonArrayRequest requast = new JsonArrayRequest(url,
new Listener<JSONArray>() {
@Override
public void onResponse(JSONArray detailJsonArray) {
// TODO Auto-generated method stub
try {
for (int i = 0; i < detailJsonArray.length(); i++) {
JSONObject obj1 = detailJsonArray
.getJSONObject(i);
String name = obj1.getString("name");
String email = obj1.getString("email");
JSONObject phone = obj1.getJSONObject("phone");
String home=phone.getString("home");
String mobile=phone.getString("mobile");
details.setEmail(email);
details.setName(name);
details.setHome(home);
details.setMobile(mobile);
addData(details);
}
} catch (Exception e) {
Log.d("Error",
"error ocures from exception............");
}
// Toast.makeText(MainActivity.this, arg0.toString(),
// Toast.LENGTH_SHORT).show();
}
}, new ErrorListener() {
@Override
public void onErrorResponse(VolleyError arg0) {
// TODO Auto-generated method stub
}
});
Volley.newRequestQueue(MainActivity.this).add(requast);
}
public void addData(Details details) {
// TODO Auto-generated method stub
detailsDao.insert(details);
}
public void getdata() {
// TODO Auto-generated method stub
ArrayList<Details> details = (ArrayList<Details>) detailsDao.loadAll();
display(details);
}
@SuppressWarnings("rawtypes")
private void display(ArrayList<Details> details) {
//show here
}
}
当我运行代码时,我得到了以下错误
07-06 11:12:33.536: E/AndroidRuntime(17841): FATAL EXCEPTION: main
07-06 11:12:33.536: E/AndroidRuntime(17841): java.lang.NoClassDefFoundError: com.example.dao.DaoMaster
07-06 11:12:33.536: E/AndroidRuntime(17841): at com.example.dao.DaoMaster$OpenHelper.onCreate(DaoMaster.java:39)
07-06 11:12:33.536: E/AndroidRuntime(17841): at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
07-06 11:12:33.536: E/AndroidRuntime(17841): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
07-06 11:12:33.536: E/AndroidRuntime(17841): at com.example.vollyexample.Vollyapp.setDao(Vollyapp.java:35)
07-06 11:12:33.536: E/AndroidRuntime(17841): at com.example.vollyexample.Vollyapp.onCreate(Vollyapp.java:28)
07-06 11:12:33.536: E/AndroidRuntime(17841): at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1065)
07-06 11:12:33.536: E/AndroidRuntime(17841): at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4640)
07-06 11:12:33.536: E/AndroidRuntime(17841): at android.app.ActivityThread.access$1300(ActivityThread.java:165)
07-06 11:12:33.536: E/AndroidRuntime(17841): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1433)
07-06 11:12:33.536: E/AndroidRuntime(17841): at android.os.Handler.dispatchMessage(Handler.java:107)
07-06 11:12:33.536: E/AndroidRuntime(17841): at android.os.Looper.loop(Looper.java:194)
07-06 11:12:33.536: E/AndroidRuntime(17841): at android.app.ActivityThread.main(ActivityThread.java:5370)
07-06 11:12:33.536: E/AndroidRuntime(17841): at java.lang.reflect.Method.invokeNative(Native Method)
07-06 11:12:33.536: E/AndroidRuntime(17841): at java.lang.reflect.Method.invoke(Method.java:525)
07-06 11:12:33.536: E/AndroidRuntime(17841): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
07-06 11:12:33.536: E/AndroidRuntime(17841): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
07-06 11:12:33.536: E/AndroidRuntime(17841): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:2)
NoClassDefFoundError 表示负责动态加载类的类加载器无法找到.class
。
使用vapp=(Vollyapp) Vollyapp.getApplicationContext();
getContext()
- 仅返回当前运行活动的上下文视图。
getApplicationContext()
- 返回在应用程序中运行的所有活动的上下文。
我不确定它是否有用。我想你会受此影响。