2014-11-05 24 views
-1

我創建了一個聊天信使。我根本沒有得到任何出。強制停止累計。請幫助自定義列表中CustomAdapter中的空指針異常

主代碼的.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

發佈你的logcat的錯誤請 – reactivemobile 2014-11-05 10:26:11

+0

你收到了什麼錯誤/異常信息?請在這裏發佈。 – 2014-11-05 10:29:28

+0

感謝您的支持。我已經解決了我的問題。我從一開始就開始了cde .. :) – 2014-11-06 09:01:28

回答

0

如果你要改變你的view,而不是你的convertview然後你必須返回它:

View view=convertView;然後返回view代替:return view;

還您TextView的內容分配應留在外面的外,如果不喜歡你這樣做(eventName.setText(name);

它應該看起來像

holder.eventName.setText(name); 

,並應留剛剛回歸方法之前。