子类SurfaceView中的findViewById引发RuntimeException

概述如果将findViewById调用移到Activity中,则此代码可以正常工作.关于为什么它不能从LearningView类内部使用的任何提示?我尝试将TextView移到com.example.LearningView标记内,但没有任何乐趣.我更希望从SurfaceView子类中获取TextView,因为我认为将电视视为SV的“子级”更为合理.这是

如果将findVIEwByID调用移到Activity中,则此代码可以正常工作.关于为什么它不能从LearningVIEw类内部使用的任何提示?我尝试将TextVIEw移到com.example.LearningVIEw标记内,但没有任何乐趣.我更希望从SurfaceVIEw子类中获取TextVIEw,因为我认为将电视视为SV的“子级”更为合理.

这是我为弄清楚较大应用程序中的问题而编写的一个示例,但其要旨是相同的,并且堆栈跟踪基本上相同.

出于其价值,findVIEwByID调用返回的是null,这显然在某些尝试解决此问题的过程中引发了NullPointerExceptions.

从我的尝试中您可能会发现,我对此一无所知.

Learning.java:

package com.example.Learning;import androID.app.Activity;import androID.os.Bundle;public class Learning extends Activity {    /** Called when the activity is first created. */    @OverrIDe    public voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.main);    }}

LearningVIEw.java:

package com.example.Learning;import androID.content.Context;import androID.util.AttributeSet;import androID.util.Log;import androID.vIEw.SurfaceHolder;import androID.vIEw.SurfaceVIEw;import androID.Widget.TextVIEw;public class LearningVIEw extends SurfaceVIEw implements SurfaceHolder.Callback {    public LearningVIEw(Context context, AttributeSet atts) {        super(context, atts);        getHolder().addCallback(this);          }    @OverrIDe    public voID surfaceChanged(SurfaceHolder holder, int format, int wIDth,            int height) {        TextVIEw t = (TextVIEw) findVIEwByID(R.ID.contents);        t.setText("testing");    }    @OverrIDe    public voID surfaceCreated(SurfaceHolder holder) {    }    @OverrIDe    public voID surfaceDestroyed(SurfaceHolder holder) {    }}

main.xml中:

<?xml version="1.0" enCoding="utf-8"?><FrameLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    androID:orIEntation="vertical" androID:layout_wIDth="fill_parent"    androID:layout_height="fill_parent">    <com.example.Learning.LearningVIEw        androID:ID="@+ID/learningvIEw" androID:layout_wIDth="fill_parent"        androID:layout_height="fill_parent" />    <TextVIEw androID:ID="@+ID/contents" androID:layout_gravity="bottom|left"        androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content"        androID:textcolor="#ffffffff" /></FrameLayout>

堆栈跟踪:

Thread [<3> main] (Suspended (exception RuntimeException))      ActivityThread.performlaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2454      ActivityThread.handleLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2470       ActivityThread.access00(ActivityThread, ActivityThread$ActivityRecord, Intent) line: 119     ActivityThread$H.handleMessage(Message) line: 1821      ActivityThread$H(Handler).dispatchMessage(Message) line: 99     Looper.loop() line: 123     ActivityThread.main(String[]) line: 4310        Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]      Method.invoke(Object, Object...) line: 521      ZygoteInit$MethodAndArgsCaller.run() line: 860      ZygoteInit.main(String[]) line: 618     NativeStart.main(String[]) line: not available [native method]  

解决方法:

您崩溃是因为在findVIEwByID()之后,您的TextVIEw为null. TextVIEw不是SurfaceVIEw的子级,因此以SurfaceVIEw为起​​点调用findVIEwByID()不会找到它.

总结

以上是内存溢出为你收集整理的子类SurfaceView中的findViewById引发RuntimeException全部内容,希望文章能够帮你解决子类SurfaceView中的findViewById引发RuntimeException所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/web/1093774.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存