我在Retriver.java类中调用MainActivity函数sendToServer(arguments),当我在mainactivity类中工作时,此函数正常工作。但是当我从retriver.java调用它时,我得到nullPointExceltion
logcat的:
12-29 02:24:07.772: E/AndroidRuntime(12958): FATAL EXCEPTION: main
12-29 02:24:07.772: E/AndroidRuntime(12958): Process: com.example.osc, PID: 12958
12-29 02:24:07.772: E/AndroidRuntime(12958): java.lang.NullPointerException
12-29 02:24:07.772: E/AndroidRuntime(12958): at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:109)
12-29 02:24:07.772: E/AndroidRuntime(12958): at com.example.osc.MainActivity$AsyncCreateUser.onPostExecute(MainActivity.java:340)
12-29 02:24:07.772: E/AndroidRuntime(12958): at com.example.osc.MainActivity$AsyncCreateUser.onPostExecute(MainActivity.java:1)
12-29 02:24:07.772: E/AndroidRuntime(12958): at android.os.AsyncTask.finish(AsyncTask.java:632)
12-29 02:24:07.772: E/AndroidRuntime(12958): at android.os.AsyncTask.access$600(AsyncTask.java:177)
12-29 02:24:07.772: E/AndroidRuntime(12958): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
12-29 02:24:07.772: E/AndroidRuntime(12958): at android.os.Handler.dispatchMessage(Handler.java:102)
12-29 02:24:07.772: E/AndroidRuntime(12958): at android.os.Looper.loop(Looper.java:136)
12-29 02:24:07.772: E/AndroidRuntime(12958): at android.app.ActivityThread.main(ActivityThread.java:5017)
12-29 02:24:07.772: E/AndroidRuntime(12958): at java.lang.reflect.Method.invokeNative(Native Method)
12-29 02:24:07.772: E/AndroidRuntime(12958): at java.lang.reflect.Method.invoke(Method.java:515)
12-29 02:24:07.772: E/AndroidRuntime(12958): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
12-29 02:24:07.772: E/AndroidRuntime(12958): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
12-29 02:24:07.772: E/AndroidRuntime(12958): at dalvik.system.NativeStart.main(Native Method)
当我将getApplicationContext()更改为MainActivity.this时,它仍然会出错并且logcat reslut就是这个。
12-29 02:43:09.829: E/AndroidRuntime(20558): FATAL EXCEPTION: main
12-29 02:43:09.829: E/AndroidRuntime(20558): Process: com.example.osc, PID: 20558
12-29 02:43:09.829: E/AndroidRuntime(20558): java.lang.NullPointerException
12-29 02:43:09.829: E/AndroidRuntime(20558): at android.content.ContextWrapper.getResources(ContextWrapper.java:89)
12-29 02:43:09.829: E/AndroidRuntime(20558): at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:78)
12-29 02:43:09.829: E/AndroidRuntime(20558): at android.widget.Toast.<init>(Toast.java:93)
12-29 02:43:09.829: E/AndroidRuntime(20558): at android.widget.Toast.makeText(Toast.java:241)
12-29 02:43:09.829: E/AndroidRuntime(20558): at com.example.osc.MainActivity$AsyncCreateUser.onPostExecute(MainActivity.java:340)
12-29 02:43:09.829: E/AndroidRuntime(20558): at com.example.osc.MainActivity$AsyncCreateUser.onPostExecute(MainActivity.java:1)
12-29 02:43:09.829: E/AndroidRuntime(20558): at android.os.AsyncTask.finish(AsyncTask.java:632)
12-29 02:43:09.829: E/AndroidRuntime(20558): at android.os.AsyncTask.access$600(AsyncTask.java:177)
12-29 02:43:09.829: E/AndroidRuntime(20558): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
12-29 02:43:09.829: E/AndroidRuntime(20558): at android.os.Handler.dispatchMessage(Handler.java:102)
12-29 02:43:09.829: E/AndroidRuntime(20558): at android.os.Looper.loop(Looper.java:136)
12-29 02:43:09.829: E/AndroidRuntime(20558): at android.app.ActivityThread.main(ActivityThread.java:5017)
12-29 02:43:09.829: E/AndroidRuntime(20558): at java.lang.reflect.Method.invokeNative(Native Method)
12-29 02:43:09.829: E/AndroidRuntime(20558): at java.lang.reflect.Method.invoke(Method.java:515)
12-29 02:43:09.829: E/AndroidRuntime(20558): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
12-29 02:43:09.829: E/AndroidRuntime(20558): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
12-29 02:43:09.829: E/AndroidRuntime(20558): at dalvik.system.NativeStart.main(Native Method)
在Retriver.java中
if (c.moveToFirst()) {
sender_name = c.getString(c.getColumnIndexOrThrow("Chidname"));
//sender_name = c.getString(c.getColumnIndex("senderName"));
MainActivity obj = new MainActivity();
if(
obj.sendToServer(c.getString(c.getColumnIndexOrThrow("Chidname")),
c.getString(c.getColumnIndexOrThrow("Chidname")),
c.getString(c.getColumnIndexOrThrow("ChildAge")),
c.getString(c.getColumnIndexOrThrow("ChildGender")),
c.getString(c.getColumnIndexOrThrow("ChildAddress")),
c.getString(c.getColumnIndexOrThrow("Sendername")),
c.getString(c.getColumnIndexOrThrow("SenderEmail")),
c.getString(c.getColumnIndexOrThrow("SenderSimNo")),
c.getString(c.getColumnIndexOrThrow("SenderContact")),
c.getString(c.getColumnIndexOrThrow("Longitude")),
c.getString(c.getColumnIndexOrThrow("Latitude")),
"get this later")){
String deleteRecored = "DELETE FROM tblOSC WHERE _Id="+c.getString(c.getColumnIndexOrThrow("_Id"));
db.execSQL(deleteRecored);
}
}
c.close();
MainActivity.java
public boolean sendToServer(String cpp, String cn,String ca,String cg, String cadd,String sn,String se,
String ssn, String sp, String log, String lat, String cpis) {
UserDetailsTable userDetail = new UserDetailsTable(cpp,
cn, ca,
cg,
cadd, sn, se,
sn, sp, log,
lat, cpis);
new AsyncCreateUser().execute(userDetail);
return SENT_TO_SERVER_FLAG;
}
protected class AsyncCreateUser extends
AsyncTask<UserDetailsTable, Void, Void> {
@Override
protected Void doInBackground(UserDetailsTable... params) {
RestAPI api = new RestAPI();
try {
api.CreateNewAccount(params[0].getchildPhotoPath(),
params[0].getchildName(), params[0].getchildAge(),
params[0].getchildGender(),
params[0].getchildAddress(), params[0].getsenderName(),
params[0].getsenderCellPhone(),
params[0].getsenderMobile(),
params[0].getsenderEmail(), params[0].getlongitude(),
params[0].getlattitude(),
params[0].childPhotoImageString);
} catch (Exception e) {
// TODO Auto-generated catch block
Log.d("AsyncCreateUser", e.getMessage());
}
return null;
}
@Override
protected void onPostExecute(Void result) {
SENT_TO_SERVER_FLAG = true;
Toast.makeText(getApplicationContext(),"sent to PEF", Toast.LENGTH_LONG).show();
}
}
答案 0 :(得分:1)
使用 - MainActivity.this
代替getApplicationContext();
@Override
protected void onPostExecute(Void result) {
SENT_TO_SERVER_FLAG = true;
Toast.makeText(MainActivity.this,"sent to PEF", Toast.LENGTH_LONG).show();
}
答案 1 :(得分:1)
在MainActivity定义中定义一个全局Context contex
变量(即在onCreate()开始之前),并在onCreate中为其赋予contex = getApplicationContext()
。
然后使用contex作为Toast参数。
Toast.makeText(contex, "sent to PEF", ...