嘿,我的问题与 android 中的意图和 asynctask 有关我有以下代码,我从按钮启动 asynctask
Button.OnClickListener buttonSignin = new Button.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
ConnectivityManager connMgr = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo == null && networkInfo.isConnected() == false) {
Toast.makeText(getApplicationContext(), "No Internet connection", Toast.LENGTH_SHORT).show();
}
else if(editUserName.getText().length() == 0 && editPassword.getText().length() == 0){
Toast.makeText(getApplicationContext(), "Please fill required details", Toast.LENGTH_SHORT).show();
}else{
enteredUserName = editUserName.getText().toString();
password = editPassword.getText().toString();
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("username", enteredUserName));
nameValuePairs.add(new BasicNameValuePair("password", password));
atn = new AsyncTaskNetwork();
atn.execute("http://staff.faharitech.com/raphael/hirizi/api/public/v1/user/login",nameValuePairs);
// new sendLoginDetails().execute("http://staff.faharitech.com/raphael/hirizi/api/public/v1/user/login");
}
asynctask 在另一个公共类中,在这里
public class AsyncTaskNetwork extends AsyncTask<Object, Integer, String> {
ConnectToAPI conn;
LoginActivity la;
@Override
protected String doInBackground(Object... params) {
// TODO Auto-generated method stub
String result = null;
try {
conn = new ConnectToAPI();
String url = (String) params[0];
List<NameValuePair> nameValuePairs = ( List<NameValuePair>) params[1];
result = conn.postData(url, nameValuePairs);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
@Override
protected void onPostExecute(String result){
la = new LoginActivity();
la.parseJsonData(result);
}
}
最后,被调用 onPostExecute 的方法与启动 asynctask 的按钮在同一个类中
public void parseJsonData(String result){
try{
JSONObject json_data = new JSONObject(result);
if(json_data != null){
JSONObject jObj = json_data.getJSONObject("user");
if(jObj != null){
JSONObject jObj2 =null;
username = jObj.getString("username");
System.out.println(username);
token = jObj.getString("token");
System.out.println(token);
fullName = jObj.getString("full name");
System.out.println(fullName);
profilePic = jObj.getString("profilePic");
System.out.println(profilePic);
System.out.println(jObj.getString("stores"));
JSONArray jArray = new JSONArray(jObj.getString("stores"));
for(int i = 0; i < jArray.length(); i++){
jObj2 = jArray.getJSONObject(i);
sName = jObj2.getString("name");
System.out.println(sName);
sId = jObj2.getString("id");
System.out.println(sId);
logo = jObj2.getString("logo");
System.out.println(logo);
}
displayResults = username + " " + token;
}
else{
JSONObject jObjMessage = json_data.getJSONObject("message");
errMessage = jObjMessage.getString("message");
displayResults = errMessage;
}
}
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
if(username != null && token != null){
Intent intent = new Intent(getApplicationContext(),NavigationActivity.class);
startActivity(intent);
}
else{
Toast.makeText(getApplicationContext(),errMessage, 10000000).show();
}
}
我的问题是在意图调用 nullpointerexception 时出现错误,这与上下文有关,除了 getApplicationContext() 之外,我应该在那里使用哪个上下文
更新日志猫
08-19 14:59:45.649: E/AndroidRuntime(6897): FATAL EXCEPTION: main
08-19 14:59:45.649: E/AndroidRuntime(6897): java.lang.NullPointerException
08-19 14:59:45.649: E/AndroidRuntime(6897): at android.content.ContextWrapper.getPackageName(ContextWrapper.java:135)
08-19 14:59:45.649: E/AndroidRuntime(6897): at android.content.ComponentName.<init>(ComponentName.java:75)
08-19 14:59:45.649: E/AndroidRuntime(6897): at android.content.Intent.<init>(Intent.java:3491)
08-19 14:59:45.649: E/AndroidRuntime(6897): at com.faharitech.hirizi.LoginActivity.parseJsonData(LoginActivity.java:336)
08-19 14:59:45.649: E/AndroidRuntime(6897): at com.faharitech.hirizi.AsyncTaskNetwork.onPostExecute(AsyncTaskNetwork.java:36)
08-19 14:59:45.649: E/AndroidRuntime(6897): at com.faharitech.hirizi.AsyncTaskNetwork.onPostExecute(AsyncTaskNetwork.java:1)
08-19 14:59:45.649: E/AndroidRuntime(6897): at android.os.AsyncTask.finish(AsyncTask.java:631)
08-19 14:59:45.649: E/AndroidRuntime(6897): at android.os.AsyncTask.access$600(AsyncTask.java:177)
08-19 14:59:45.649: E/AndroidRuntime(6897): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
08-19 14:59:45.649: E/AndroidRuntime(6897): at android.os.Handler.dispatchMessage(Handler.java:99)
08-19 14:59:45.649: E/AndroidRuntime(6897): at android.os.Looper.loop(Looper.java:137)
08-19 14:59:45.649: E/AndroidRuntime(6897): at android.app.ActivityThread.main(ActivityThread.java:5041)
08-19 14:59:45.649: E/AndroidRuntime(6897): at java.lang.reflect.Method.invokeNative(Native Method)
08-19 14:59:45.649: E/AndroidRuntime(6897): at java.lang.reflect.Method.invoke(Method.java:511)
08-19 14:59:45.649: E/AndroidRuntime(6897): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-19 14:59:45.649: E/AndroidRuntime(6897): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-19 14:59:45.649: E/AndroidRuntime(6897): at dalvik.system.NativeStart.main(Native Method)