Android FirebaseException:无法退回到键入

时间:2017-05-30 18:13:46

标签: android firebase firebase-realtime-database

我创建了android聊天应用程序,当我想打开聊天时,我得到错误应用程序已经停止工作并且在日志里面我得到异常: ' Firebase例外:未能退回键入'  为什么会发生这种错误?

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=} }

1 个答案:

答案 0 :(得分:2)

在投射JSON对象时,Firebase的家伙会对此类问题提供全面的解释here

所以我猜你的是使用转换Map对象类型。

我没有尝试,而是替换下面的行

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

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

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

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

应该有用。