【发布时间】:2018-11-12 12:02:05
【问题描述】:
我想在 TextView 中显示文本。我有一个 .txt 存储在 Firebase 存储中。我将此 .txt 的 Url 存储在我的 Android Studio 项目(main/res/raw)中的 .xml 中
这是活动中 onCreate 的一些代码。
str = poi.getText();
final Uri uri = Uri.parse(str);
TextView textView = findViewById(R.id.TEXT_STATUS_ID);
textView.setText((CharSequence) uri);
但是我遇到了问题:
Process: com.example.project, PID: 10040
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.project/com.example.project.POIPresentationActivity}: java.lang.ClassCastException: android.net.Uri$StringUri cannot be cast to java.lang.CharSequence
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2984)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3045)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1642)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6776)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386)
Caused by: java.lang.ClassCastException: android.net.Uri$StringUri cannot be cast to java.lang.CharSequence
at com.example.project.POIPresentationActivity.onCreate(POIPresentationActivity.java:117)
at android.app.Activity.performCreate(Activity.java:6958)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2927)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3045)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1642)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6776)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386)
看起来我无法将 Uri 转换为 CharSequence。实现我想做的最好的方法是什么?我的方法适用于图像、音频和视频(因为不需要演员表)
str = poi.getVideoLink();
Uri uri = Uri.parse(str);
VideoView video = findViewById(R.id.video_view);
video.setMediaController(new MediaController(this));
video.setVideoURI(uri);
video.requestFocus();
我看过一些关于 inputStreams 的文档,但不确定这是否是可行的方法。任何见解表示赞赏
使用建议的 toString() 方法无法按预期工作:
textView.setText((CharSequence) uri);
改为
textView.setText(uri.toString());
显示文本,但它显示我要显示的文本的链接 (https://firebasestorage.googleapis.com/ ......) - 不是链接指向的 .txt 中包含的文本
【问题讨论】:
-
改用
toString() -
请查看更新的问题,谢谢
标签: java firebase android-studio firebase-realtime-database