findViewById在子类SurfaceView抛出的RuntimeException [英] findViewById in a subclassed SurfaceView throwing RuntimeException

查看:119
本文介绍了findViewById在子类SurfaceView抛出的RuntimeException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这code工作正常,如果我移动findViewById呼叫的活动。任何提示,为什么不从LearningView类内部的工作?我已经试过移动com.example.LearningView标记,但没有喜悦里面的TextView。我想preFER从SurfaceView子类中获得TextView的,因为我觉得它更合乎逻辑对待电视为SV的孩子。

这是一个人为的例子,我写弄清楚这个问题在一个较大的应用程序,但它的主要内容是一样的,堆栈跟踪是基本一致的。

对于什么是值得的,在findViewById调用返回null,这显然扔NullPointerException异常的一些尝试解决这个问题。

正如你可能会从我的努力,我在这一个盲目聚集地。

Learning.java:

 包com.example.Learning;

进口android.app.Activity;
进口android.os.Bundle;

公共类学习延伸活动{
    / **第一次创建活动时调用。 * /
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);
    }
}
 

LearningView.java:

 包com.example.Learning;

进口android.content.Context;
进口android.util.AttributeSet;
进口android.util.Log;
进口android.view.SurfaceHolder;
进口android.view.SurfaceView;
进口android.widget.TextView;

公共类LearningView扩展了SurfaceView实现SurfaceHolder.Callback {
    公共LearningView(上下文的背景下,AttributeSet中的ATT){
        超(背景下,的ATT);
        。getHolder()的addCallback(本);
    }

    @覆盖
    公共无效surfaceChanged(SurfaceHolder持有人,INT格式,诠释的宽度,
            INT高度){
        TextView的T =(TextView中)findViewById(R.id.contents);
        t.se​​tText(测试);
    }

    @覆盖
    公共无效surfaceCreated(SurfaceHolder持有者){
    }

    @覆盖
    公共无效surfaceDestroyed(SurfaceHolder持有者){
    }
}
 

main.xml中:

 < XML版本=1.0编码=UTF-8&GT?;
<的FrameLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:方向=垂直机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT>
    < com.example.Learning.LearningView
        机器人:ID =@ + ID / learningview机器人:layout_width =FILL_PARENT
        机器人:layout_height =FILL_PARENT/>
    < TextView的机器人:ID =@ + ID /内容机器人:layout_gravity =底部|左
        机器人:layout_width =WRAP_CONTENT机器人:layout_height =WRAP_CONTENT
        机器人:文字颜色=#FFFFFFFF/>
< /的FrameLayout>
 

堆栈跟踪:

 发[3;>主](暂停(RuntimeException的除外))
    ActivityThread.performLaunchActivity(ActivityThread $ ActivityRecord,意图)线:2454
    ActivityThread.handleLaunchActivity(ActivityThread $ ActivityRecord,意图)线:2470
    ActivityThread.access $ 2200(ActivityThread,ActivityThread $ ActivityRecord,意图)线:119
    ActivityThread $ H.handleMessage(消息)线:1821
    ActivityThread $ H(处理器).dispatchMessage(消息)线:99
    Looper.loop()线:123
    ActivityThread.main(字符串[])线:4310
    Method.invokeNative(对象,对象[],上课,下课[],类,整型,布尔)行:不可用[本地方法]
    Method.invoke(对象,对象...)线:521
    ZygoteInit $ MethodAndArgsCaller.run()线:860
    ZygoteInit.main(字符串[])线:618
    NativeStart.main(字符串[])行:不可用[本地方法]
 

解决方案

正在崩溃,因为你的TextView是findViewById后空()。 TextView的不是SurfaceView的孩子,因此调用findViewById()与SurfaceView为出发点,将找不到它。

This code works fine if I move the findViewById call in to the Activity. Any hints as to why it doesn't work from inside the LearningView class? I've tried moving the TextView inside the com.example.LearningView tag but no joy. I'd prefer to get the TextView from within the SurfaceView subclass as I feel it's more logical to treat the TV as a "child" of the SV.

This is a contrived example I wrote to figure out the problem in a larger application, but the gist of it is the same, and the stack trace is basically the same.

For what it is worth, the findViewById call is returning null, which obviously threw NullPointerExceptions in some attempts to fix this.

As you can probably gather from my attempts I'm flying blind on this one.

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>

Stack trace:

Thread [<3> main] (Suspended (exception RuntimeException))  
    ActivityThread.performLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2454  
    ActivityThread.handleLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2470   
    ActivityThread.access$2200(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]  

解决方案

You are crashing because your TextView is null after findViewById(). The TextView is not a child of the SurfaceView, therefore calling findViewById() with the SurfaceView as the starting point will not find it.

这篇关于findViewById在子类SurfaceView抛出的RuntimeException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆