2016-01-25 54 views
-1

我想在我的列表中添加一些基於當前用戶的用戶名的特定ParseObject。但這個代碼只結束了在NullPointerException異常上線:如何指定要添加到列表中的parseObject <parseObject>

query.findInBackground(新FindCallback()

mStatus.add(status.get(I));

有人請幫我解決這個問題。謝謝。

@Nullable 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View view = null; 
    view = inflater.inflate(R.layout.update_status, container, false); 

    ParseUser currentUser = ParseUser.getCurrentUser(); 
    final String strCurrentUsername = currentUser.getUsername(); 
    if (currentUser != null) { 

     //try 
     final ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Status"); 
     query.orderByDescending("createdAt"); 

     query.findInBackground(new FindCallback<ParseObject>() { 
      @Override 
      public void done(final List<ParseObject> status, ParseException e) { 
       if(e == null){ 
        //success 
        for(int i = 0; i <status.size();i++){ 
         String usernow = status.get(i).getString("user"); 
         if(usernow.equals(strCurrentUsername)){ 
          Toast.makeText(getActivity(), "num"+i, Toast.LENGTH_SHORT).show(); 
          mStatus.add(status.get(i)); 
         } 
         else i++; 
        } 
        //mStatus = status; 
        StatusAdapter statusAdapter = new StatusAdapter(getListView().getContext(), mStatus); 
        setListAdapter(statusAdapter); 

       }else{ 
        //there was a problem 

       } 
      } 
     }); 


    } else { 
     Intent intent = new Intent(getActivity(), FirstActivity.class); 
     startActivity(intent); 
    } 

    return view; 
} 
+0

是mStatus initalized? –

回答

0

是MSTATUS initalized?是MSTATUS類字段?我不知道你在哪裏得到空例外,但我測試你的代碼,這個工作對我來說,其中用戶在我的解析數據庫中的列它的值是一個字符串:

ParseUser currentUser = ParseUser.getCurrentUser(); 
    final String strCurrentUsername = currentUser.getUsername(); 
    if (currentUser != null) { 

     //try 
     final ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Status"); 
     query.orderByDescending("createdAt"); 

     query.findInBackground(new FindCallback<ParseObject>() { 
      @Override 
      public void done(final List<ParseObject> status, ParseException e) { 
       if (e == null) { 
        ArrayList<ParseObject> mStatus = new ArrayList<ParseObject>(); 
        //success 
        for (int i = 0; i < status.size(); i++) { 
         String usernow = status.get(i).getString("user"); 
         if (usernow.equals(strCurrentUsername)) { 

          mStatus.add(status.get(i)); 
         } else i++; 
        } 


       } else { 
        //there was a problem 

       } 
      } 
     }); 


    } else { 

    } 
+0

是的.. mStatus被初始化爲一個受保護的列表 ..這很奇怪。我仍然無法修復錯誤。 –

+0

你有沒有試過讓它公開?嗯,零點來自哪裏? mStatus或用戶?你的解析類的ACL是什麼?無論如何, –

+0

,我解決了它。感謝你和這條線ArrayList mStatus = new ArrayList (); 也許.add不適用於列表。謝謝! –

相關問題