我的Android项目上有这个界面:
package com.kkoci.shairlook;
/**
* Created by kristian on 07/07/2015.
*/
public interface OnTaskFinishListener{
void onFinish();
}
我正在使用它,在我的AsyncTask类上调用onPostExecute
:
package com.kkoci.shairlook;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;
import com.appspot.shairlook1.userEndpoint.UserEndpoint;
import com.appspot.shairlook1.userEndpoint.model.User;
import com.google.api.client.extensions.android.http.AndroidHttp;
import com.google.api.client.extensions.android.json.AndroidJsonFactory;
import com.google.api.client.googleapis.services.AbstractGoogleClientRequest;
import com.google.api.client.googleapis.services.GoogleClientRequestInitializer;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
/**
* Created by kristian on 04/07/2015.
*/
public class EndpointsAsyncTaskInsert extends AsyncTask<String, Void, User> implements GoogleClientRequestInitializer {
private static UserEndpoint myApiService = null;
private Context context;
private OnTaskFinishListener listener;
EndpointsAsyncTaskInsert(Context context) {
this.context = context;
}
public EndpointsAsyncTaskInsert(OnTaskFinishListener listener){
this.listener = listener;
}
@Override
public void initialize(AbstractGoogleClientRequest<?> abstractGoogleClientRequest) throws IOException {
// put it here no in MyClass
abstractGoogleClientRequest.setDisableGZipContent(true);
}
@Override
protected User doInBackground(String... params) {
User response = null;
if (myApiService == null) { // Only do this once
UserEndpoint.Builder builder = new UserEndpoint.Builder(AndroidHttp.newCompatibleTransport(),
new AndroidJsonFactory(), null)
// options for running against local devappserver
// - 10.0.2.2 is localhost's IP address in Android emulator
// - turn off compression when running against local devappserver
.setRootUrl("https://shairlook1.appspot.com/_ah/api/")
.setGoogleClientRequestInitializer(this);
// end options for devappserver
myApiService = builder.build();
}
try {
User users = new User();
users.setEmail(params[0]);
users.setPassword(params[1]);
users.setName(params[2]);
response = myApiService.insertUser(users).execute();
} catch (Exception e) {
Log.d("Could not Add User", e.getMessage(), e);
}
return response;
}
protected void onPostExecute(User user){
listener.onFinish();
}
}
但是这给了我java.lang.NoCalssDefFound
错误,我在我的活动中初始化它是这样的:
public class LoginMember extends Activity implements OnTaskFinishListener {
public void onFinish(){
Intent intent = new Intent(LoginMember.this, WelcomeScreen.class);
startActivity(intent);
}
然后执行:
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(txtEmail.getWindowToken(), 0);
imm.hideSoftInputFromWindow(txtPassword.getWindowToken(), 0);
String password = txtPassword.getText().toString();
String email = txtEmail.getText().toString();
if ((txtEmail.length() == 0) || (txtPassword.length() == 0)) {
Toast.makeText(LoginMember.this, "You need to provide values for Email and Password", Toast.LENGTH_SHORT).show();
return;
}
//Go ahead and perform the transaction
String[] params = {email, password};
new EndpointsAsyncTaskInsert(currentActivity.getApplicationContext()).execute(params);
}
});
因此,执行EndpointAsyncTaskInsert
方法listener.OnFinish();
这是完整的logcat:
7752-7752/com.kkoci.shairlook E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at com.kkoci.shairlook.EndpointsAsyncTaskInsert.onPostExecute(EndpointsAsyncTaskInsert.java:74)
at com.kkoci.shairlook.EndpointsAsyncTaskInsert.onPostExecute(EndpointsAsyncTaskInsert.java:24)
at android.os.AsyncTask.finish(AsyncTask.java:631)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:175)
at android.app.ActivityThread.main(ActivityThread.java:5279)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
我在这里看到这个question似乎是同一类问题,但我对此解决方案并不十分确定。
请问任何想法?
提前致谢!
答案 0 :(得分:2)
日志说错误是java.lang.NullPointerException
...而且它是因为您使用EndpointsAsyncTaskInsert
构造函数创建了Context
,因此{{1} }在其内部null ...并且在不listener