嘿我的问题与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();
}
}
我的问题是在intent调用上出现错误nullpointerexception与上下文有关,除了getApplicationContext()
之外我应该使用上下文更新logcat
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)
答案 0 :(得分:0)
你必须使用你的活动名称dot,sample:
YourActivity.this
答案 1 :(得分:0)
独自敬酒吗? 如果其他答案不起作用,请尝试在活动类中定义asynctask。(它应该)
答案 2 :(得分:0)
制作AsyncTaskNetwork
的构造函数并以Context
作为参数。将AsyncTaskNetwork
的成员变量声明为Context mContext
并在构造函数中初始化。即。
public class AsyncTaskNetwork extends AsyncTask<Object, Integer, String> {
ConnectToAPI conn;
LoginActivity la;
Context mContext;
public AsyncTaskNetwork(Context context){
this.mContext=context;
}
.........
}
在调用Intent
时,您可以传递此上下文。即。
Intent intent = new Intent(mContext,NavigationActivity.class);
startActivity(intent);