gpt4 book ai didi

java - AsyncTask 接口(interface)中的 NullPointerException

转载 作者:行者123 更新时间:2023-12-01 12:23:01 26 4
gpt4 key购买 nike

我希望实现一个 AsyncTask,并实现一个与 AsyncTask 的接口(interface),以将创建的用户对象传输到 AsyncTask .execute() 之后启动的新 Activity。

该问题似乎是由接口(interface)引起的。我对数据库发送/接收数据没有任何问题。

我是否没有声明导致 NullPointerException 的结果?有人看到任何实现不正确的地方吗?

创建用户

    public static class CreateUser extends AsyncTask<User, Void, User> {

Context mContext;

public interface CreateUserInterface {

public void getCreatedUser(User user);

public void getUserCreateErrorMessage(String error);

}

CreateUserInterface callback;

public CreateUser(Context context) {
// TODO Auto-generated constructor stub
this.mContext = context;
// callback = (CreateUserInterface) context;
}

@Override
protected User doInBackground(User... params) {
ObjectMapper mapper = new ObjectMapper(); // create once, reuse
User user = params[0];
String url = ROUTE_USER_CREATE;
HttpPost httppost = new HttpPost(url);
HttpClient httpclient = new DefaultHttpClient();
String UserJSONResponse = null;

try {
String jsonString = mapper.writeValueAsString(user);
StringEntity m_stringEntity = new StringEntity(jsonString);

httppost.setEntity(m_stringEntity);
httppost.addHeader("Content-type", "application/json");

HttpResponse postResponse = httpclient.execute(httppost);

UserJSONResponse = EntityUtils.toString(postResponse
.getEntity());
user = mapper.readValue(UserJSONResponse, User.class);

} catch (JsonProcessingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return user;
}

@Override
protected void onPostExecute(User result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
//User user = new User();
Log.e("USERNAME", result.getUser_name());
callback.getCreatedUser(result);

}
}

这是我尝试从中检索用户并实现接口(interface)的 Activity

public class UserFragment extends Fragment implements OnClickListener, UserREST.CreateUser.CreateUserInterface {

UserREST.CreateUser createUserAsyncTask;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup content_frame,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_user, content_frame, false);
createUserAsyncTask = new UserREST.CreateUser(getActivity());

Spinner spinner = (Spinner) view.findViewById(R.id.spinnerUserChoice);
// Create an ArrayAdapter using the string array and a default spinner
// layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
getActivity(), R.array.user_choice_array,
android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);

setHasOptionsMenu(true);
return view;

}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// TODO Add your menu entries here
// inflater = getActivity().getMenuInflater();
inflater.inflate(R.menu.user_navigation, menu);
super.onCreateOptionsMenu(menu, inflater);

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_createUser:
// update the main content by replacing fragments
FragmentManager fragmentManager = getFragmentManager();
Fragment mCreateUserFragment = new CreateUserFragment();
fragmentManager.beginTransaction()
.replace(R.id.content_frame, mCreateUserFragment).commit();

}
return true;
}

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {

default:
break;

}

}

@Override
public void getCreatedUser(User user) {
// TODO Auto-generated method stub
Log.e("USERNAME", user.getUser_name());

}

@Override
public void getUserCreateErrorMessage(String error) {
// TODO Auto-generated method stub

}

堆栈跟踪:

    10-23 14:16:08.265: E/AndroidRuntime(463): FATAL EXCEPTION: main
10-23 14:16:08.265: E/AndroidRuntime(463): Process: com.e.main, PID: 463
10-23 14:16:08.265: E/AndroidRuntime(463): java.lang.NullPointerException
10-23 14:16:08.265: E/AndroidRuntime(463): at com.e.rest.UserREST$CreateUser.onPostExecute(UserREST.java:166)
10-23 14:16:08.265: E/AndroidRuntime(463): at com.e.rest.UserREST$CreateUser.onPostExecute(UserREST.java:1)
10-23 14:16:08.265: E/AndroidRuntime(463): at android.os.AsyncTask.finish(AsyncTask.java:632)
10-23 14:16:08.265: E/AndroidRuntime(463): at android.os.AsyncTask.access$600(AsyncTask.java:177)
10-23 14:16:08.265: E/AndroidRuntime(463): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
10-23 14:16:08.265: E/AndroidRuntime(463): at android.os.Handler.dispatchMessage(Handler.java:102)
10-23 14:16:08.265: E/AndroidRuntime(463): at android.os.Looper.loop(Looper.java:136)
10-23 14:16:08.265: E/AndroidRuntime(463): at android.app.ActivityThread.main(ActivityThread.java:5105)
10-23 14:16:08.265: E/AndroidRuntime(463): at java.lang.reflect.Method.invokeNative(Native Method)
10-23 14:16:08.265: E/AndroidRuntime(463): at java.lang.reflect.Method.invoke(Method.java:515)
10-23 14:16:08.265: E/AndroidRuntime(463): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)

最佳答案

这是我为解决此问题而进行的更改。

CreateUser 内部

        public CreateUser(Context context) {
// TODO Auto-generated constructor stub
this.mContext = context;
callback = (CreateUserInterface) context;
}

以及我正在使用的 NavigationActivity

public class NavigationActivity extends ActionBarActivity implements UserREST.CreateUser.CreateUserInterface

及其相关方法

    @Override
public void getCreatedUser(User user) {
// TODO Auto-generated method stub
Log.e("USERNAME", user.getUser_name());

}

@Override
public void getUserCreateErrorMessage(String error) {
// TODO Auto-generated method stub

}

关于java - AsyncTask 接口(interface)中的 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26534717/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com