Reputation: 177
I have a BroadcastReceiver, implemented in an Activity Class, which accepts incoming messages via Google Cloud Message. I want to use this class as a chat, so after i have incoming messages, I use a 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;
}
As i said i collect the incoming messages, and try to create a layout so that i can show them. My ChatAdapter handles this. The problem is that i get an error when i try to get the messages from the BroadcastReceiver. I use this line,
Chatpat(message_name, message_chat);
to collect the incoming messages:
String message_name = intent.getExtras().getString(NAME_MESSAGED);
String message_chat = intent.getExtras().getString(CHAT_MESSAGED);
Hopefully i wish that my adapter would be able, after that, to create a ListView which shows the message i got, and so on. The problem is that i get an error and the app crashes:
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
Because of that i also get a NullPointerException inside Chatpat void. If i remove:
Chatpat(message_name, message_chat);
from inside the BroadcastReceiver, the messages are coming in the correct way without any problem.. I do not understand why this happens. I believe that the problem has to be with the fact that i use that incoming messages to initiate Chatpat, wich create a list view to show the messages.
The really weird thing is that i tried to feed the Chatpat, with other sources and though it works, if i try to print out a value, i get the same Error again, about the receiving Intent.
This is the whole Activity:
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();
}
}
These are the classes that i use to create the list view to show the text i want in the activity:
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!!!!!!");
}
}
and:
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;
}
}
The LogCat:
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
Upvotes: 0
Views: 603
Reputation: 18151
In Chatpat you have chatinput.add(name);
but chatInput
is declared as
ArrayList<String> chatinput = null;
and it is not instantiate anywhere in your activity. you should declare it as ArrayList<String> chatinput = new ArrayList<String>();
Upvotes: 1