我创建了一个聊天信使。我根本没有得到任何出局。强制停止累积。请帮忙
主要代码.java
import com.google.android.gms.gcm.GoogleCloudMessaging;
public class Send extends Activity {
static ListView msgList;
Button send;
EditText typeText;
String Rid, name;
static ArrayList<String> msg = new ArrayList<String>();
static CustomAdapter adapter;
Image image;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.send);
SharedPreferences settings = getSharedPreferences("prefs", 0);
Rid = settings.getString("regId", "");
name = settings.getString("user_name", "");
msgList = (ListView) findViewById(R.id.listView1);
typeText = (EditText) findViewById(R.id.msg);
String last_msg = getIntent().getExtras().getString("txt");
msg.add("a");
msg.add("b");
msg.add("c");
msg.add("d");
msg.add("e");
msg.add("f");
msg.add("g");
msg.add("h");
msg.add("i");
adapter = new CustomAdapter(Send.this, msg);
msgList.setAdapter(adapter);
((Button) findViewById(R.id.send))
.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ArrayList<String> regIds = new ArrayList<String>();
regIds.add(Rid);
// if you want more devices to receive this message just
// add their regID
// in this arrayList
GCMMessageSender sender = new GCMMessageSender();
sender.send(sender.createContent(name, typeText
.getText().toString(), regIds, Rid, image));
}
});
}
public static class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
// Explicitly specify that GcmMessageHandler will handle the intent.
ComponentName comp = new ComponentName(context.getPackageName(),
GcmMessageHandler.class.getName());
// Start the service, keeping the device awake while it is
// launching.
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
};
public static class GcmMessageHandler extends IntentService {
String mes, text, RId, type, reg, group_name;
ArrayList<String> reg_list = new ArrayList<String>();
String active;
private Handler handler;
public GcmMessageHandler() {
super("GcmMessageHandler");
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
handler = new Handler();
}
@Override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
// The getMessageType() intent parameter must be the intent you
// received
// in your BroadcastReceiver.
String messageType = gcm.getMessageType(intent);
type = extras.getString("type");
mes = extras.getString("title");
text = extras.getString("message");
RId = extras.getString("Rid");
ActivityManager am = (ActivityManager) this
.getSystemService(ACTIVITY_SERVICE);
// get the info from the currently running task
List<ActivityManager.RunningTaskInfo> taskInfo = am
.getRunningTasks(1);
Log.d("topActivity", "CURRENT Activity ::"
+ taskInfo.get(0).topActivity.getClassName());
ComponentName componentInfo = taskInfo.get(0).topActivity;
active = componentInfo.getClassName();
if (type.equals("message"))
notifyMessage();
Log.i("GCM",
"Received : (" + messageType + ") "
+ extras.getString("title"));
GcmBroadcastReceiver.completeWakefulIntent(intent);
}
public void insertToList(String text) {
msg.add(text);
// adapter.updateReceiptsList(msg);
adapter.notifyDataSetChanged();
//msgList.setSelection(msg.size());
}
public void notifyMessage() {
handler.post(new Runnable() {
public void run() {
Toast.makeText(getApplicationContext(), active,
Toast.LENGTH_LONG).show();
if (active.equals("com.example.notification.Send")) {
insertToList(text);
}else{
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
GcmMessageHandler.this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(mes)
.setContentText(text)
.setSound(
RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setAutoCancel(true);
// // Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(GcmMessageHandler.this,
Send.class);
resultIntent.putExtra("txt", text);
resultIntent.putExtra("regId", RId);
resultIntent
.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
TaskStackBuilder stackBuilder = TaskStackBuilder
.create(GcmMessageHandler.this);
stackBuilder.addParentStack(Send.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent pendingintent = PendingIntent.getActivity(
GcmMessageHandler.this, 0, resultIntent, 0);
PendingIntent resultPendingIntent = stackBuilder
.getPendingIntent(0,
PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// // mId allows you to update the notification later on.
mNotificationManager.notify(0, mBuilder.build());
}
}
});
}
}
}
尝试使用自定义阵列适配器显示字符串列表。但是每次我运行我的应用程序时都会收到错误
这是我的自定义列表适配器类:
CustomAdapter.java
package com.example.notification;
public class CustomAdapter extends BaseAdapter {
ArrayList <String> msg;
ArrayList <String> date;
ArrayList<Image> image;
Activity context;
public CustomAdapter(Activity context, ArrayList<String> msg) {
// TODO Auto-generated constructor stub
//super(context, msg);
this.context=context;
this.msg=msg;
}
public void updateReceiptsList(ArrayList<String> newlist) {
this.msg.clear();
this.msg = newlist;
this.notifyDataSetChanged();
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return msg.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View view=convertView;
UserHolder holder = null;
if (view == null) {
holder = new UserHolder();
String name = msg.get(position);
if (position % 2 == 0) {
LayoutInflater inflater = (context).getLayoutInflater();
view = inflater.inflate(R.layout.msg_list_row, parent, false);
holder.eventName = (TextView) view.findViewById(R.id.msgTextView);
} else {
LayoutInflater inflater = (context).getLayoutInflater();
view = inflater.inflate(R.layout.row, parent, false);
holder.eventName = (TextView) view.findViewById(R.id.event);
}
eventName.setText(name);
view.setTag(holder);
}else {
holder = (UserHolder) view.getTag();
}
return convertView;
}
static class UserHolder {
TextView eventName;
}
}
答案 0 :(得分:0)
如果您要更改view
而不是convertview
,则必须将其退回:
View view=convertView;
然后返回view
:return view;
此外,您的textview内容分配应保留在外部,如果您不这样做(eventName.setText(name);
)
应该看起来像
holder.eventName.setText(name);
并且应该在返回方法之前停留。