Android系统。解析查询中的错误

时间:2015-08-01 05:59:28

标签: android parse-platform firebase firebase-realtime-database

我正在构建一个聊天应用程序,我正在使用Firebase来存储和传输消息,而Parse则存储所有用户数据,如(用户注册,登录,存储最近的表和发送推送)。

在Parse中,我将所有用户登录数据存储在Usertable中,并且我维护一个Recent表来存储所有最近的聊天。 用户一对一聊天将保存在一个GroupId(String)下。该组ID是通过添加

形成的
GroupId = UserOneObjectId+UserTwoObjectId;

如果有人与特定的人进行了新的聊天,它会在Recent表中存储有关最近聊天的数据,主要是它必须将GroupId和成员详细信息存储在数组中以用于exmp:

members = [UserOneObjectId, UserTwoObjectId] 

下次如果用户再次与同一个人聊天,则必须查询该表并查找成员数组。如果两个人物对象ID已经存在,则必须从该行获得 GroupId

为此我添加了这些代码。但这不是很好的检查,并告诉我我犯的是什么错误......

我的完整代码是......

public class StartChat extends AppCompatActivity {

private String FIREBASE_URL = "https://craking-fire-base.cracked.com/";

private String user1ObjectId, user1Name;
private String user2ObjectId, user2Name;
private static String groupId;

private Firebase mFirebaseRef;
private Chat chat;
private ChatListAdapter mChatListAdapter;
private ValueEventListener mConnectedListener;
private PrefManager pref;
private Toolbar mtoolbar;

private ListView listView;
private EditText inputText;

String groupIdGot;

ParseObject entryToRecentChat;

ParseQuery<ParseObject> groupIdQuery;

public StartChat() {
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_start_chat);

    mtoolbar = (Toolbar) findViewById(R.id.home_toolbar);
    setSupportActionBar(mtoolbar);
    getSupportActionBar().setDisplayShowHomeEnabled(false);
    mtoolbar.setTitle(user2Name);

    pref = new PrefManager(getApplicationContext());

    user1ObjectId = pref.getObjectId();
    user1Name = pref.getUserName();

    Intent i = getIntent();
    user2ObjectId = i.getStringExtra("user2ObjectId");
    user2Name = i.getStringExtra("user2Name");


    listView = (ListView) findViewById(R.id.list);

    /*groupId = user1ObjectId + user2ObjectId;*/

    groupIdQuery = ParseQuery.getQuery("Recent");
    groupIdQuery.whereContainsAll("members", Arrays.asList(user1ObjectId, user2ObjectId));
    Log.d("objectArray", Arrays.asList(user1ObjectId, user2ObjectId).toString());
    groupIdQuery.findInBackground(new FindCallback<ParseObject>() {
        @Override
        public void done(List<ParseObject> list, ParseException e) {
            if (e == null)
            {
                for (ParseObject dealsObject : list) {
                    // use dealsObject.get('columnName') to access the properties of the Deals object.
                    String objId = dealsObject.getObjectId();
                    groupIdGot = (String) dealsObject.get("groupId");
                    Log.d("GroupId***Got", groupIdGot);
                    //String members = (String) dealsObject.get("members");
                    Toast.makeText(getApplicationContext(), "ObjectId: " + objId + "\n" + "GroupId: " + groupId + " value present", Toast.LENGTH_LONG).show();

                }

            }else{
                Toast.makeText(getApplicationContext(), " No Data present", Toast.LENGTH_LONG).show();
                groupId = user1ObjectId + user2ObjectId;

                entryToRecentChat = new ParseObject("Recent");
                entryToRecentChat.put("groupId", groupId);
                entryToRecentChat.put("lastUser", ParseObject.createWithoutData("_User", user1ObjectId));
                entryToRecentChat.put("user", ParseObject.createWithoutData("_User", user2ObjectId));
                entryToRecentChat.addAllUnique("members", Arrays.asList(user1ObjectId, user2ObjectId));
                entryToRecentChat.saveEventually(new SaveCallback() {
                    @Override
                    public void done(ParseException e) {
                        if (e == null) {
                            Toast.makeText(getApplicationContext(), "Value saved", Toast.LENGTH_LONG).show();
                        } else {
                            Toast.makeText(getApplicationContext(), "Value not saved", Toast.LENGTH_LONG).show();
                            Log.d("exception", e.getMessage());
                        }
                    }
                });
            }
        }
    });
    //groupId = user1ObjectId + user2ObjectId;
    if(groupIdGot != null){
        groupId = groupIdGot;
    }else{
        groupId = user1ObjectId + user2ObjectId;
    }

    mFirebaseRef = new Firebase(FIREBASE_URL).child("Message").child(groupId);

    inputText = (EditText) findViewById(R.id.messageInput);
    inputText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
            if (actionId == EditorInfo.IME_NULL && keyEvent.getAction() == KeyEvent.ACTION_DOWN) {
                sendMessage();
            }
            return true;
        }
    });

    //input = inputText.getText().toString();
    findViewById(R.id.sendButton).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            sendMessage();
            sendPush();
        }
    });


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_start_chat, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@Override
protected void onStart() {
    super.onStart();

    // Setup our view and list adapter. Ensure it scrolls to the bottom as data changes
    //final ListView listView = getListView();
    mtoolbar.setTitle(user2Name);
    mChatListAdapter = new ChatListAdapter(mFirebaseRef.limit(50), this, R.layout.chat_message, user1Name);
    listView.setAdapter(mChatListAdapter);
    mChatListAdapter.registerDataSetObserver(new DataSetObserver() {
        @Override
        public void onChanged() {
            super.onChanged();
            listView.setSelection(mChatListAdapter.getCount() - 1);
        }
    });

    // Finally, a little indication of connection status
    mConnectedListener = mFirebaseRef.getRoot().child(".info/connected").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            boolean connected = (Boolean) dataSnapshot.getValue();
            if (connected) {
                Toast.makeText(StartChat.this, "Connected to ChatRoom", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(StartChat.this, "Disconnected from ChatRoom", Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {
            // No-op
        }
    });


}

@Override
protected void onStop() {
    super.onStop();
    mFirebaseRef.getRoot().child(".info/connected").removeEventListener(mConnectedListener);
    mChatListAdapter.cleanup();
}

private void sendMessage() {
    String input = inputText.getText().toString();
    if (!input.equals("")) {
        // Create our 'model', a Chat object
        chat = new Chat(input, user1Name);
        // Create a new, auto-generated child of that chat location, and save our chat data there
        mFirebaseRef.push().setValue(chat);
        inputText.setText("");
    }
}

特别是我在这一行中得到了nullpointer错误

 mFirebaseRef = new Firebase(FIREBASE_URL).child("Message").child(groupId);

因为对我来说这个查询对我不起作用。

    groupIdQuery = ParseQuery.getQuery("Recent");
    groupIdQuery.whereContainsAll("members", Arrays.asList(user1ObjectId, user2ObjectId));
    Log.d("objectArray", Arrays.asList(user1ObjectId, user2ObjectId).toString());
    groupIdQuery.findInBackground(new FindCallback<ParseObject>() {
        @Override
        public void done(List<ParseObject> list, ParseException e) {
            if (e == null)
            {
                for (ParseObject dealsObject : list) {
                    // use dealsObject.get('columnName') to access the properties of the Deals object.
                    String objId = dealsObject.getObjectId();
                    groupIdGot = (String) dealsObject.get("groupId");
                    Log.d("GroupId***Got", groupIdGot);
                    //String members = (String) dealsObject.get("members");
                    Toast.makeText(getApplicationContext(), "ObjectId: " + objId + "\n" + "GroupId: " + groupId + " value present", Toast.LENGTH_LONG).show();

                }

            }else{
                Toast.makeText(getApplicationContext(), " No Data present", Toast.LENGTH_LONG).show();
                groupId = user1ObjectId + user2ObjectId;

                entryToRecentChat = new ParseObject("Recent");
                entryToRecentChat.put("groupId", groupId);
                entryToRecentChat.put("lastUser", ParseObject.createWithoutData("_User", user1ObjectId));
                entryToRecentChat.put("user", ParseObject.createWithoutData("_User", user2ObjectId));
                entryToRecentChat.addAllUnique("members", Arrays.asList(user1ObjectId, user2ObjectId));
                entryToRecentChat.saveEventually(new SaveCallback() {
                    @Override
                    public void done(ParseException e) {
                        if (e == null) {
                            Toast.makeText(getApplicationContext(), "Value saved", Toast.LENGTH_LONG).show();
                        } else {
                            Toast.makeText(getApplicationContext(), "Value not saved", Toast.LENGTH_LONG).show();
                            Log.d("exception", e.getMessage());
                        }
                    }
                });
            }
        }
    });
    //groupId = user1ObjectId + user2ObjectId;
    if(groupIdGot != null){
        groupId = groupIdGot;
    }else{
        groupId = user1ObjectId + user2ObjectId;
    }

1 个答案:

答案 0 :(得分:1)

在你的问题中你说:

  

特别是我在这一行中得到了nullpointer错误

 mFirebaseRef = new Firebase(FIREBASE_URL).child("Message").child(groupId);

当您尝试在NullPointerException值上调用方法时,会引发null

"SO".toString(); // works
null.toString(); // throws a NullPointerException

我一直记得这个:

  

如果在点

之前有NullPointerException值,则会引发null

您的语句中有两个点,因此可以抛出NullPointException的两个位置。让我们将这句话分成几行:

 mFirebaseRef = 
     new Firebase(FIREBASE_URL) // can this be null?
         .child("Message")      // can this be null?
         .child(groupId);

因此new Firebase(FIREBASE_URL)返回null,或child("Messages")返回null。鉴于Firebase的工作原理,child("Message")很可能返回null。因此,您的数据库没有名为Message的子节点。