Android:更新GCM上的ListView收到崩溃我的应用程序

时间:2015-12-26 11:14:02

标签: android listview google-cloud-messaging android-adapter

非常感谢您的帮助,当我收到GCM推送通知时,我的应用程序一直崩溃,当我尝试更新我的listView时崩溃,这里是完整的代码:

GCM课程(完整):

public class MyGcmListenerService extends GcmListenerService {

private static final String TAG = "MyGcmListenerService";

/**
 * Called when message is received.
 *
 * @param from SenderID of the sender.
 * @param data Data bundle containing message data as key/value pairs.
 *             For Set of keys use data.keySet().
 */
// [START receive_message]
@Override
public void onMessageReceived(String from, Bundle data) {
    String message = data.getString("m");
    Log.d(TAG, "From: " + from);
    Log.d(TAG, "Message: " + message);

    if(myDbHelper.checkDataBase()) {
        DataBaseHelper gsh = new DataBaseHelper(getApplicationContext());
        gsh.getSQLiteHistory();
    }

    sendNotification(message);

}
// [END receive_message]

/**
 * Create and show a simple notification containing the received GCM message.
 *
 * @param message GCM message received.
 */

private void sendNotification(String message) {
    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);

    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_stat_ic_notification)
            .setContentTitle("my alert")
            .setContentText(message)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
}

从SQL类我调用" new AlertsList()。GCMRefresh(rowItems);"

现在,当我有我的项目列表时,我在AlertList类中调用GCMRefresh:

public void GCMRefresh(List<RowItem> rowItems)
{
    adapter = new CustomListViewAdapter(this.getActivity(),
                R.layout.list_item, rowItems); //CRASH HERE
    mList.setAdapter(adapter);
}

添加了AlertList类: 公共类AlertsList extends Fragment实现OnTaskCompleted {

...
public void setContext(Context context){
    mContext = context;
}

public AlertsList getListener(){
    return this;
}

ArrayList<String> title_array = new ArrayList<String>();
ArrayList<String> notice_array = new ArrayList<String>();

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    setHasOptionsMenu(true);

    View v = inflater.inflate(R.layout.alerts_list, container,
            false);
   mList = (ListView) v.findViewById(R.id.list);

    DataBaseHelper myDbHelper = new DataBaseHelper(this.getContext());
    if(myDbHelper.checkDataBase()) {
        getSQLiteHistory();
    }

    return v;
}

CustomListViewAdapter类:

public class CustomListViewAdapter extends ArrayAdapter<RowItem> {

    Context context;

    public CustomListViewAdapter(Context context, int resourceId,
                                 List<RowItem> items) {
        super(context, resourceId, items);
        this.context = context;
    }

    /*private view holder class*/
    private class ViewHolder {
        ImageView imageView;
        TextView txtTitle;
        TextView txtDesc;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        RowItem rowItem = getItem(position);

        LayoutInflater mInflater = (LayoutInflater) context
                .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.list_item, null);
            holder = new ViewHolder();
            holder.txtDesc = (TextView) convertView.findViewById(R.id.desc);
            holder.txtTitle = (TextView) convertView.findViewById(R.id.title);
            holder.imageView = (ImageView) convertView.findViewById(R.id.icon);
            convertView.setTag(holder);
        } else
            holder = (ViewHolder) convertView.getTag();

        holder.txtDesc.setText(rowItem.getDesc());
        holder.txtTitle.setText(rowItem.getTitle());
        holder.imageView.setImageResource(rowItem.getImageId());

        return convertView;
    }
}

那么,如何更新我的listView而不会让我的应用程序崩溃?

以下是崩溃错误: 尝试调用虚方法&#39; java.lang.Object android.content.Context.getSystemService(java.lang.String)&#39;在null对象引用上 在死线程上向处理程序发送消息。

谢谢!

叶兰。

1 个答案:

答案 0 :(得分:0)

感谢@Dinesh Kannan,以下是答案:  this.getActivity()在CustomListViewAdapter中为null(this.getActivity()) 获得mContext后,一切正常:

adapter = new CustomListViewAdapter(mContext,R.id.list,rowItems);