2017-05-30 61 views
0

我创建了Android聊天应用程序,当我想打开聊天时,出现错误应用程序已停止工作,并且内部日志中出现异常: 'Firebase异常:未能弹出以键入' 为什么会发生这种错误?Android FirebaseException:无法弹出以输入

public class Chat extends AppCompatActivity 
{ 
    LinearLayout layout; 
    RelativeLayout layout_2; 
    ImageView sendButton; 
    EditText messageArea; 
    ScrollView scrollView; 
    Firebase reference1, reference2; 
    boolean timerProcessing = false; 
    String date = ""; 
    String message; 
    String userName; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_chat); 

     layout = (LinearLayout) findViewById(R.id.layout1); 
     layout_2 = (RelativeLayout)findViewById(R.id.layout2); 
     sendButton = (ImageView)findViewById(R.id.sendButton); 
     messageArea = (EditText)findViewById(R.id.messageArea); 
     scrollView = (ScrollView)findViewById(R.id.scrollView); 

     Firebase.setAndroidContext(this); 

     reference1 = new Firebase("https://zipa1x.firebaseio.com/messages/" + UserDetails.username + "_" + UserDetails.chatWith); 
     reference2 = new Firebase("https://zipa1x.firebaseio.com/messages/" + UserDetails.chatWith + "_" + UserDetails.username); 

     sendButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       String messageText = messageArea.getText().toString(); 

       if(!messageText.equals("")){ 
        Map<String, String> map = new HashMap<String, String>(); 
        map.put("message", messageText); 
        map.put("user", UserDetails.username); 
        reference1.push().setValue(map); 
        reference2.push().setValue(map); 
        messageArea.setText(""); 
       } 
      } 
     }); 

     reference1.addChildEventListener(new ChildEventListener() 
     { 
      @Override 
      public void onChildAdded(DataSnapshot dataSnapshot, String s) 
      { 
        Map map = dataSnapshot.getValue(Map.class); 
        message = map.get("message").toString(); 
        userName = map.get("user").toString(); 

        if (userName.equals(UserDetails.username)) 
        { 
         //addMessageBox("You:\n" + message, 1); 
         addMessageBox(message, 1); 

         //sendNotification(message,userName); 
        } else 
        { 
         //addMessageBox(UserDetails.chatWith + ":\n" + message, 2); 
         addMessageBox(message, 2); 

         //sendNotification(message,userName); 
        } 
      } 

      @Override 
      public void onChildChanged(final DataSnapshot dataSnapshot, String s) 
      { 
       Bundle extras = getIntent().getExtras(); 
       if (extras != null) 
       { 
        int value = extras.getInt("time"); 

        final CountDownTimer timer = new CountDownTimer(value, 1) 
        { 
         public void onTick(long millisUntilFinished) 
         { 
         } 

         public void onFinish() 
         { 
          timerProcessing = false; 

          for (DataSnapshot child: dataSnapshot.getChildren()) 
          { 
           dataSnapshot.getRef().removeValue(); 
          } 
         } 
        }; 
       } 
      } 

      @Override 
      public void onChildRemoved(DataSnapshot dataSnapshot) { 

      } 

      @Override 
      public void onChildMoved(DataSnapshot dataSnapshot, String s) { 

      } 

      @Override 
      public void onCancelled(FirebaseError firebaseError) { 

      } 
     }); 

     scrollView.post(new Runnable() { 
      @Override 
      public void run() { 
       scrollView.fullScroll(View.FOCUS_DOWN); 
      } 
     }); 
    } 

    public void addMessageBox(String message, int type) 
    { 
     sendNotification(message); 

     DateFormat df = new SimpleDateFormat("h:mm a"); 
     date = df.format(Calendar.getInstance().getTime()); 

     TextView textView1 = new TextView(Chat.this); 
     textView1.setText(date); 
     textView1.setTextSize(10); 

     TextView textView = new TextView(Chat.this); 
     textView.setText(message); 

     LinearLayout.LayoutParams lp2 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); 
     lp2.weight = 1.0f; 

     if(type == 1) { 
      lp2.gravity = Gravity.RIGHT; 
      textView.setBackgroundResource(R.drawable.left_bubble); 
     } 
     else{ 
      lp2.gravity = Gravity.LEFT; 
      textView.setBackgroundResource(R.drawable.right_bubble); 
     } 
     textView.setLayoutParams(lp2); 
     layout.addView(textView); 

     textView1.setLayoutParams(lp2); 
     layout.addView(textView1); 

     scrollView.post(new Runnable() { 
      @Override 
      public void run() { 
       scrollView.fullScroll(View.FOCUS_DOWN); 
      } 
     }); 
    } 

    private void sendNotification(String message) 
    { 
     Intent intent = new Intent(this,Chat.class); 
     intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

     PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT); 

     Uri notificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 

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

     NotificationCompat.Builder builder = (NotificationCompat.Builder) new NotificationCompat.Builder(this) 
       .setVisibility(Notification.VISIBILITY_PUBLIC) 
       //.setOngoing(true) 
       .setSmallIcon(R.mipmap.ic_launcher) 
       .setContentTitle(userName) 
       .setContentText(message) 
       .setSound(notificationSound) 
       .setContentIntent(pendingIntent); 

     NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
     mNotificationManager.notify(0, builder.build()); 
    } 

的logcat:

05-30 23:52:56.264 31908-31908/audiophileradio.example.com.audiophileradio 
    E/AndroidRuntime: FATAL EXCEPTION: main Process: audiophileradio.example.com.audiophileradio, PID: 31908 
                           com.firebase.client.FirebaseException: Failed to bounce to type 
    at com.firebase.client.DataSnapshot.getValue(DataSnapshot.java:185) 
    at audiophileradio.example.com.audiophileradio.Chat$2.onChildAdded(Chat.java:99) 
    at com.firebase.client.core.ChildEventRegistration.fireEvent(ChildEventRegistration.java:61) 
    at com.firebase.client.core.view.DataEvent.fire(DataEvent.java:45) 
    at com.firebase.client.core.view.EventRaiser$1.run(EventRaiser.java:38) 
    at android.os.Handler.handleCallback(Handler.java:733) 
    at android.os.Handler.dispatchMessage(Handler.java:95) 
    at android.os.Looper.loop(Looper.java:136) 
    at android.app.ActivityThread.main(ActivityThread.java:5476) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:515) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099) 
    at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132) 
    at dalvik.system.NativeStart.main(Native Method) 

    Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not instantiate value of type [map type; class java.util.LinkedHashMap, [simple type, class java.lang.Object] -> [simple type, class java.lang.Object]] from String value; no single-String constructor/factory method 
    at com.fasterxml.jackson.databind.deser.std.StdValueInstantiator._createFromStringFallbacks(StdValueInstantiator.java:428) 
    at com.fasterxml.jackson.databind.deser.std.StdValueInstantiator.createFromString(StdValueInstantiator.java:299) 
    at com.fasterxml.jackson.databind.deser.std.MapDeserializer.deserialize(MapDeserializer.java:308) 
    at com.fasterxml.jackson.databind.deser.std.MapDeserializer.deserialize(MapDeserializer.java:26) 
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2888) 
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2034) 
    at com.firebase.client.DataSnapshot.getValue(DataSnapshot.java:183) 
    at audiophileradio.example.com.audiophileradio.Chat$2.onChildAdded(Chat.java:99) 
    at com.firebase.client.core.ChildEventRegistration.fireEvent(ChildEventRegistration.java:61) 
    at com.firebase.client.core.view.DataEvent.fire(DataEvent.java:45) 
    at com.firebase.client.core.view.EventRaiser$1.run(EventRaiser.java:38) 
    at android.os.Handler.handleCallback(Handler.java:733) 
    at android.os.Handler.dispatchMessage(Handler.java:95) 
    at android.os.Looper.loop(Looper.java:136) 
    at android.app.ActivityThread.main(ActivityThread.java:5476) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:515) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099) 
    at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132) 
    at dalvik.system.NativeStart.main(Native Method) 
    android firebase 
    shareeditflag 

https://postimg.org/image/3wn46dxzr/

https://postimg.org/image/5pq0upj6f/

06-07 00:23:47.679 8103-8103/audiophileradio.example.com.audiophileradio D/dalvikvm: VFY: replacing opcode 0x5b at 0x005d 
06-07 00:23:47.679 8103-8103/audiophileradio.example.com.audiophileradio D/dalvikvm: DexOpt: couldn't find field Landroid/app/Notification;.headsUpContentView 
06-07 00:23:47.679 8103-8103/audiophileradio.example.com.audiophileradio W/dalvikvm: VFY: unable to resolve instance field 20 
06-07 00:23:47.679 8103-8103/audiophileradio.example.com.audiophileradio D/dalvikvm: VFY: replacing opcode 0x5b at 0x004c 
06-07 00:23:47.679 8103-8103/audiophileradio.example.com.audiophileradio D/dalvikvm: DexOpt: couldn't find field Landroid/app/Notification;.headsUpContentView 
06-07 00:23:47.679 8103-8103/audiophileradio.example.com.audiophileradio I/dalvikvm: DexOpt: unable to optimize instance field ref 0x0014 at 0x54 in Landroid/support/v7/app/NotificationCompat;.addHeadsUpToBuilderLollipop 
06-07 00:23:47.679 8103-8103/audiophileradio.example.com.audiophileradio D/dalvikvm: DexOpt: couldn't find field Landroid/app/Notification;.headsUpContentView 
06-07 00:23:47.679 8103-8103/audiophileradio.example.com.audiophileradio I/dalvikvm: DexOpt: unable to optimize instance field ref 0x0014 at 0x61 in Landroid/support/v7/app/NotificationCompat;.addHeadsUpToBuilderLollipop 
06-07 00:23:47.699 8103-8103/audiophileradio.example.com.audiophileradio I/System.out: datasnaphot: DataSnapshot { key = -KlzVQr5A15Y6Is3qKCE, value = {message=b, user=} } 
06-07 00:23:47.699 8103-8103/audiophileradio.example.com.audiophileradio E/datasnaphot:: DataSnapshot { key = -KlzVQr5A15Y6Is3qKCE, value = {message=b, user=} } 
06-07 00:23:52.089 8103-8103/audiophileradio.example.com.audiophileradio I/System.out: datasnaphot: DataSnapshot { key = -KlzVRvdHnLr3MYweJ0x, value = {message=v, user=} } 
06-07 00:23:52.089 8103-8103/audiophileradio.example.com.audiophileradio E/datasnaphot:: DataSnapshot { key = -KlzVRvdHnLr3MYweJ0x, value = {message=v, user=} } 
+0

添加错误的完整输出并注意引起此错误的行将有所帮助。看起来像一个类似的问题在这里回答:https://stackoverflow.com/a/33923921/4816918 – Jeff

+0

我已更新问题与日志下面的代码和错误是在这一行:Map map = dataSnapshot.getValue(Map.class); – pavlestojanovic1906

+0

看起来像你有确切的问题,上面链接的答案解决 – Jeff

回答

1

铸造JSON对象时火力地堡家伙给出了一个全面的解释here这个有点问题。

所以我想你的是与铸造地图对象类型。

我没有尝试,但与

Map<String, String> map = (Map<String, String>) dataSnapshot.getValue(); 

message = map.get("message"); 
userName = map.get("user"); 

替换以下行

Map map = dataSnapshot.getValue(Map.class); 

message = map.get("message").toString(); 
userName = map.get("user").toString(); 

应该工作。

+0

当我将代码更改为你的代码时,我得到异常:java.lang.ClassCastException:java.lang.String不能转换为java.util.Map – pavlestojanovic1906

+0

显示你的数据库结构。 – uguboz

+0

我在问题里面添加了两个用于我的firebase数据库的链接! – pavlestojanovic1906