-1

嘿,我的问题与 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)
4

3 回答 3

0

单独吐司有用吗?如果其他答案不起作用,请尝试在活动类中定义 asyntask。(应该)

于 2013-08-19T14:59:50.367 回答
0

你必须使用你的活动名称点这个,示例:

YourActivity.this
于 2013-08-19T14:56:28.293 回答
0

制作你的构造函数AsyncTaskNetworkContext作为参数。AsyncTaskNetwork声明一个as的成员变量Context mContext并在构造函数中初始化。IE

public class AsyncTaskNetwork extends AsyncTask<Object, Integer, String> {
    ConnectToAPI conn;
    LoginActivity la;
    Context mContext;


public AsyncTaskNetwork(Context context){

    this.mContext=context;
  }
 .........
}

在调用的时候Intent,你可以传递这个上下文。IE

Intent intent = new Intent(mContext,NavigationActivity.class);
        startActivity(intent);   
于 2013-08-19T15:03:45.897 回答