我正在从活动(android)调用java类中的db。是否可以从activity调用java类。我使用它进行服务器通信android。我从服务器获取数据,但我无法插入数据库。
我这样打电话就遇到了异常,任何人都可以帮助我,在此先感谢。
11-01 06:52:03.596: W/System.err(673): java.lang.NullPointerException
11-01 06:52:03.603: W/System.err(673): at com.commiunication.ServerConnection.Insert(ServerConnection.java:32)
11-01 06:52:03.627: W/System.err(673): at com.commiunication.MainActivity.serverconnection(MainActivity.java:138)
11-01 06:52:03.627: W/System.err(673): at com.commiunication.MainActivity.onCreate(MainActivity.java:46)
11-01 06:52:03.633: W/System.err(673): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-01 06:52:03.633: W/System.err(673): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
11-01 06:52:03.633: W/System.err(673): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-01 06:52:03.633: W/System.err(673): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-01 06:52:03.633: W/System.err(673): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-01 06:52:03.633: W/System.err(673): at android.os.Handler.dispatchMessage(Handler.java:99)
11-01 06:52:03.633: W/System.err(673): at android.os.Looper.loop(Looper.java:123)
11-01 06:52:03.633: W/System.err(673): at android.app.ActivityThread.main(ActivityThread.java:4627)
11-01 06:52:03.633: W/System.err(673): at java.lang.reflect.Method.invokeNative(Native Method)
11-01 06:52:03.655: W/System.err(673): at java.lang.reflect.Method.invoke(Method.java:521)
11-01 06:52:03.655: W/System.err(673): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-01 06:52:03.655: W/System.err(673): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-01 06:52:03.655: W/System.err(673): at dalvik.system.NativeStart.main(Native Method)
androidactivity.java
ServerConnection server=new ServerConnection();
try {
server.Insert(ReturnValue);
sampleDB = this.openOrCreateDatabase(SAMPLE_DB_NAME,MODE_PRIVATE,null);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
serverConnection.java
public void Insert(String ReturnValue) throws JSONException{
JSONObject CDGSGJson=new JSONObject(ReturnValue);
JSONArray idarr = CDGSGJson.getJSONArray("MeasureArr");
System.out.println(""+idarr.length());
sampleDB= openOrCreateDatabase(SAMPLE_DB_NAME, SQLiteDatabase.OPEN_READWRITE, null);
System.out.println(""+idarr.length());
//Toast.makeText(this, "Group Details Downloading", Toast.LENGTH_SHORT).show();
try {
sampleDB.execSQL("CREATE TABLE IF NOT EXISTS " + table_name + " "
+ "( id INTEGER PRIMARY KEY AUTOINCREMENT," + " measure TEXT NOT NULL ); ");
if(idarr.length()>0){
for(int cc=0;cc<idarr.length();cc++){
JSONObject CatObj = idarr.getJSONObject(cc);
String id_get=CatObj.getString("id");
String measure_get=CatObj.getString("measure");
sampleDB.execSQL("INSERT OR REPLACE INTO " + table_name+ " Values
('"+id_get+"','"+measure_get+"');");
}}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
// System.out.print(e.printStackTrace());
}