0

我有一个广播接收器,在一个活动类中实现,它通过谷歌云消息接受传入的消息。我想将此类用作聊天,因此在收到传入消息后,我使用 void:

public void Chatpat(String name, String message){

        chatinput.add(name);
        chatinput.add(message);


        Chat chat_data[] = new Chat[chatinput.size()];
        chatinput.toArray();


        ChatAdapter adapter = new ChatAdapter(this, 
                R.layout.listview_item_row, chat_data);

        chatList = (ListView) findViewById(R.id.listView1);
        chatList.setAdapter(adapter);

        return;
    }

正如我所说,我收集传入的消息,并尝试创建一个布局以便我可以显示它们。我的 ChatAdapter 处理这个。问题是当我尝试从 BroadcastReceiver 获取消息时出现错误。我用这条线,

Chatpat(message_name, message_chat);

收集传入的消息:

String message_name = intent.getExtras().getString(NAME_MESSAGED);
String message_chat = intent.getExtras().getString(CHAT_MESSAGED);

希望我希望我的适配器在那之后能够创建一个 ListView 来显示我收到的消息,等等。问题是我收到错误并且应用程序崩溃:

03-25 22:22:09.928: E/AndroidRuntime(1010): java.lang.RuntimeException: Error receiving broadcast Intent { act=com.cabman.lol.ok.DISPLAY_MESSAGE (has extras) } in com.cabman.lol.ok.UserComActivity$1@44ee7778

因此,我还在 Chatpat void 中得到了 NullPointerException。如果我删除:

Chatpat(message_name, message_chat);

从广播接收器内部,消息以正确的方式传入,没有任何问题。我不明白为什么会发生这种情况。我相信问题在于我使用传入的消息来启动 Chatpat,从而创建一个列表视图来显示消息。

真正奇怪的是,我试图用其他来源提供 Chatpat,虽然它有效,但如果我尝试打印出一个值,我会再次得到相同的错误,关于接收 Intent。

这是整个活动:

public class UserComActivity extends Activity{
List<String> messages;
List<String> names;
private ListView chatList;
SessionManager session;
EditText userText;
EditText chatText;
UserFunctions user = new UserFunctions();
String informchat = "yes";
String message_gcm;
Boolean i = false;
Chat chat_data[] ;
ArrayList<String> chatinput = null;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.userchat);


    registerReceiver(mHandleMessageReceiver, new IntentFilter(
            DISPLAY_MESSAGE_ACTION));

    //chatText = (EditText) findViewById(R.id.messageHistory);
    userText = (EditText) findViewById(R.id.message);






    final String regId = GCMRegistrar.getRegistrationId(this);
    final Button button = (Button) findViewById(R.id.sendMessageButton);
    session = new SessionManager(getApplicationContext());
    HashMap<String, String> comu = session.matchUserDetails();


    button.setOnClickListener(new View.OnClickListener() {



        @Override
        public void onClick(View arg0) {

            String message = userText.getText().toString();


            if (i == false){
     session = new SessionManager(getApplicationContext());
     HashMap<String, String> comu = session.matchUserDetails();
             String gcm = comu.get(SessionManager.KEY_GCM);
             HashMap<String, String> co = session.getUserDetails();
             String name = co.get(SessionManager.KEY_NAME);

             System.out.println("UserComActivity:");
             System.out.println(name);
             System.out.println(gcm);

             chatText.setText(name +": "+ message);

          //   user.sendMessage(name, gcm, regId, message);
            }
            else if (i == true){


            session = new SessionManager(getApplicationContext());
            HashMap<String, String> co = session.getUserDetails();
            String name = co.get(SessionManager.KEY_NAME); 

         //   user.sendMessage(name, message_gcm, regId, message);

            }
        }
});

}



/**
public ArrayList<String> addChat(String name, String message){


    chatinput.add(name);
    chatinput.add(message);

    return chatinput;



}**/

    public void Chatpat(String name, String message){

        chatinput.add(name);
        chatinput.add(message);




        Chat chat_data[] = new Chat[chatinput.size()];
        chatinput.toArray(chat_data);




        ChatAdapter adapter = new ChatAdapter(this, 
                R.layout.listview_item_row, chat_data);

        chatList = (ListView) findViewById(R.id.listView1);
        chatList.setAdapter(adapter);

        return;
    }



private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {


        i = true;
        String message_tag = intent.getExtras().getString(TAG_MESSAGE);
        if(message_tag.equals("chat")){

        String message_name = intent.getExtras().getString(NAME_MESSAGED);
       String message_chat = intent.getExtras().getString(CHAT_MESSAGED);




         Chatpat(message_name, message_chat);

        }

    }
};

@Override
protected void onDestroy() {

    try {
        unregisterReceiver(mHandleMessageReceiver);
        GCMRegistrar.onDestroy(this);
    } catch (Exception e) {
        Log.e("UnRegister Receiver Error", "> " + e.getMessage());
    }
    super.onDestroy();
}

}

这些是我用来创建列表视图以在活动中显示我想要的文本的类:

public class Chat {

public String name;
public String message;
public Chat(){
    super();
}

public Chat(String name, String message) {
    super();
    this.name = name;
    this.message = message;
    //System.out.println(name);
    System.out.println("Chat!!!!!!");
}

}

和:

public class ChatAdapter extends ArrayAdapter<Chat>{

Context context; 
int layoutResourceId;    
Chat data[] = null;


    public ChatAdapter(Context context, int layoutResourceId, 
Chat[] data) {
    super(context, layoutResourceId, data);
    this.layoutResourceId = layoutResourceId;
    this.context = context;
    this.data = data;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    ChatHolder holder = null;

    if(row == null)
    {
        LayoutInflater inflater = ((Activity)context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);

        holder = new ChatHolder();
        holder.txtUName = (TextView)row.findViewById(R.id.userName);
        holder.txtMessage = (TextView)row.findViewById(R.id.txtChat);
        System.out.println("ChatAdapter");
        row.setTag(holder);
    }
    else
    {
        holder = (ChatHolder)row.getTag();
    }

    Chat chat = data[position];
    holder.txtMessage.setText(chat.message);
    holder.txtUName.setText(chat.name);
    System.out.println("ChatAdapter");
    return row;
}

static class ChatHolder
{

    TextView txtUName;
    TextView txtMessage;
}

}

日志猫:

03-26 02:29:28.807: E/AndroidRuntime(5063): FATAL EXCEPTION: main
03-26 02:29:28.807: E/AndroidRuntime(5063): java.lang.RuntimeException: Error receiving     broadcast Intent { act=com.cabman.lol.ok.DISPLAY_MESSAGE (has extras) } in   com.cabman.lol.ok.UserComActivity$1@44e877d0
03-26 02:29:28.807: E/AndroidRuntime(5063):     at android.app.ActivityThread$PackageInfo$ReceiverDispatcher$Args.run(ActivityThread.java:905)
03-26 02:29:28.807: E/AndroidRuntime(5063):     at android.os.Handler.handleCallback(Handler.java:587)
03-26 02:29:28.807: E/AndroidRuntime(5063):     at android.os.Handler.dispatchMessage(Handler.java:92)
03-26 02:29:28.807: E/AndroidRuntime(5063):     at android.os.Looper.loop(Looper.java:123)
03-26 02:29:28.807: E/AndroidRuntime(5063):     at android.app.ActivityThread.main(ActivityThread.java:4627)
03-26 02:29:28.807: E/AndroidRuntime(5063):     at java.lang.reflect.Method.invokeNative(Native Method)
03-26 02:29:28.807: E/AndroidRuntime(5063):     at java.lang.reflect.Method.invoke(Method.java:521)
03-26 02:29:28.807: E/AndroidRuntime(5063):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-26 02:29:28.807: E/AndroidRuntime(5063):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-26 02:29:28.807: E/AndroidRuntime(5063):     at dalvik.system.NativeStart.main(Native Method)
03-26 02:29:28.807: E/AndroidRuntime(5063): Caused by: java.lang.ArrayStoreException
03-26 02:29:28.807: E/AndroidRuntime(5063):     at java.lang.System.arraycopy(Native Method)
03-26 02:29:28.807: E/AndroidRuntime(5063):     at java.util.ArrayList.toArray(ArrayList.java:523)
03-26 02:29:28.807: E/AndroidRuntime(5063):     at com.cabman.lol.ok.UserComActivity.Chatpat(UserComActivity.java:149)
03-26 02:29:28.807: E/AndroidRuntime(5063):     at com.cabman.lol.ok.UserComActivity$1.onReceive(UserComActivity.java:184)
03-26 02:29:28.807: E/AndroidRuntime(5063):     at android.app.ActivityThread$PackageInfo$ReceiverDispatcher$Args.run(ActivityThread.java:892)
03-26 02:29:28.807: E/AndroidRuntime(5063):     ... 9 more
4

1 回答 1

1

在 Chatpat 中,您拥有chatinput.add(name);chatInput被声明为
ArrayList<String> chatinput = null;,并且它不会在您的活动中的任何地方实例化。你应该把它声明为ArrayList<String> chatinput = new ArrayList<String>();

于 2013-03-25T23:51:21.077 回答