为什么我得到null而不是Map对象?如何解决这个问题?

时间:2013-03-06 12:02:35

标签: java android android-asynctask nullpointerexception

我正在编写一个Android应用程序,遇到了一个问题:我得到了一个null而不是ImmutableMap对象。看来我的地图正在被垃圾收集。但我创建了它的引用。所以这里是我的所有源代码,在评论中详细描述了问题:

//I have a BaseActivity class in which i defined nested class
//which incapsulates some REST operations
public class BaseActivity extends Activity
 {   
//Here is this ImmutableMap to which I want save the result
private ImmutableMap<String, Object> serverData_;

//Here is this nested class which is AsyncTask
class RestfulNetworkTasks extends AsyncTask
    <Pair<String,LinkedHashMap<String, Object>>, Void, LinkedHashMap<String, Object>>
{    
     NetworkOperation LowLevelRestOperations_ = new NetworkOperation(); 
    @Override
    protected LinkedHashMap<String, Object> doInBackground(Pair<String,LinkedHashMap<String, Object>>... params)    
   { 
    return  LowLevelRestOperations_.executeServerCommand
          (params[0].getValue0(), params[0].getValue1());
    }
    protected void onPostExecute(LinkedHashMap<String, Object> result)
    {  
        //Here i created a reference to the result(in order
        //to save it in my data structure)
        serverData_ = copyOf(result);
        UpdateCurrentActivityAfterNetworkOperation();
        //Here I made a test output to check if i got the result
        //All is good - I got a correct result saved to serverData_
        android.util.Log.d("TEST RESPONSE", "ON POST" + serverData_);
    }
}//End of nested class declaration
   //Here is my method in which i want to call AsyncTask and got a problem
   public void  Login(LinkedHashMap<String,Object> parameters)
   {   
//Here I create an instance of that nested AsyncTask class
RestfulNetworkTasks serverOperation_ = new RestfulNetworkTasks();
Pair<String, LinkedHashMap<String, Object>> params_ = 
new Pair<String,LinkedHashMap<String,Object>>(LOGIN_COMMAND, parameters);
//Here I call AsyncTask operation - i got a correct ON POST output
     //with correct data
serverOperation_.execute(params_);

//But from this point my serverData_ is null!
//And I got null in AFTER POST output!Why?serverData_ is a member of BaseActivity so 
//it have to keep the result.
android.util.Log.d("TEST RESPONSE", "AFTER POST" + serverData_);
}

那么如何解决这个问题呢?调用execute后为什么我的serverData_为null?

我真的需要帮助 - 我是Java和Android的新手,这个问题让我发疯了。提前谢谢。

4 个答案:

答案 0 :(得分:2)

  调用execute?

后,我的serverData_为null

因为 AsyncTasks在单个后台线程上串行执行而没有在UI线程(如线程)上等待,直到线程执行没有完成。

如果要在主ui线程上等待,直到AsyncTasks计算未完成并返回结果,则使用AsyncTask.get()

示例:

serverOperation_.execute(params_).get();  //<<<< 

// now u get latest value in serverData_
android.util.Log.d("TEST RESPONSE", "AFTER POST" + serverData_);

答案 1 :(得分:1)

  

看来我的地图正在被垃圾收集。

不,那不是问题。您可能使用的对象不会被垃圾回收。永远不会。

真正的问题是在使用变量之前没有赋值。

答案 2 :(得分:0)

似乎对象serverData_尚未初始化,在您的代码中,您调用了serverData_ = copyOf(result);线程中的语句,可能在将来执行,之后您使用的serverData_将为null,直到onPostExecute结束。请等到AsyncTask完成。

答案 3 :(得分:0)

问题是ImmutableMap。如果这是谷歌版本,一旦创建它不能改变。一种解决方案是使用常规java库函数来分配和构建Map,然后使用Collections接口返回和不可修改的视图;类似的东西:

HashMap<String, Object> modifiableMap = new HashMap<String, Object>();   
modifiableMap.put("1", new Object());   
modifiableMap.put("2", new Object());   
Map<String, Object> unmodifiableMap = Collections.unmodifiableMap(modifiableMap);   

或者,你可以坚持使用谷歌实现,但你需要使用他们的构建方法;类似的东西:

ImmutableMap<String, Object> map = ImmutableMap.builder()
    .put(key, value);
    .build();