android - 如何在我的类(class)中找到 FrameLayout

标签 android xml android-framelayout

我有一个 XML 文件,其中有一个 FrameLayout,其中有另一个 FrameLayout

我的类(class),我想显示屏幕并隐藏另一个屏幕内的 FrameLayout ,当用户单击按钮时,FrameLayout 被隐藏出现,但我收到错误。

是否可以通过findViewById在另一个Framelayout中找到这个Framelayout

我的代码:

XML:

    <?xml version="1.0" encoding="utf-8"?>

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textViewTimer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="90dp"
        android:layout_gravity="center_horizontal"
        android:textColor = "@color/white"
        android:text="."
        android:textSize="70sp" />

  <ImageView
    android:id="@+id/imageViewwaves_startScreen"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    android:src="@drawable/waves" />

    <FrameLayout
        android:id="@+id/teamInfoLayout"
        android:layout_width="275dp"
        android:layout_height="300dp" 
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="160dp"
        android:background="@color/white"
        >

         <TextView
        android:id="@+id/textViewMyTeams"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginBottom="80dp"
        android:textSize="20sp"
        android:text="My Teams:"
          />

           <TextView
        android:id="@+id/textViewClickOnTeamName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="30dp"
        android:textSize="13sp"
        android:text="(Click on a team name to race for that team)"
        />


    </FrameLayout>

</FrameLayout>

类(class):

ImageView waves;
View layout;
LayoutParams layoutParams, layoutParamsTextView;
FrameLayout layoutteamInfo;
private static float dx =0, dy=0, x= 0, y=0;


public ShowInfo(Context context) {
    super(context);
    waves = (ImageView) findViewById(R.id.imageViewwaves_startScreen);
    layoutteamInfo = new FrameLayout(getContext());
    layoutteamInfo = (FrameLayout) findViewById(R.id.teamInfoLayout);
}


public void hideImages()
{

    layoutteamInfo.setVisibility(View.INVISIBLE);
    waves.setVisibility(View.INVISIBLE); //Waves are gone 

}

因评论而更新:

所以,我的 logcats 显示:

06-06 16:42:33.800: E/AndroidRuntime(2073): Caused by: java.lang.NullPointerException 06-06 16:42:33.800: E/AndroidRuntime(2073): at com.qwantech.workshape.ShowInfo.hideImages(ShowInfo.java:33) 06-06 16:42:33.800: E/AndroidRuntime(2073): at com.qwantech.workshape.Start.onCreate(Start.java:47)

我创建了另一个类“ShowInfo extends View”来处理图像和布局。

这些代码就在那里。

在我的“Starts”类(class)中,我创建了一个对象:

ShowInfo showObj = new ShowInfo(getContext());

并在 StartonCreate 中调用:

showObj.hideImages();

我在调用此函数时收到错误。

最佳答案

由于您正在使用另一个类,因此您需要传递 View 的根元素才能使用 findViewById()。否则,您的类将脱离上下文并且无法访问 View 元素。

mRootView.findViewById(R.id.teamInfoLayout) 应该可以工作。

关于android - 如何在我的类(class)中找到 FrameLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24090184/

相关文章:

android - 在 Ubuntu 中通过 npm 安装 appium 时出现构建错误

java - 找不到 PreferenceScreen 类

Android: RelativeLayout inside FrameLayout, width height 错误

java - 是否可以将按钮添加到以编程方式设置的框架布局?

java - 共享重复方法的单独对象

android - 访问为触摸监听器动态创建的层

android - 在 Activity.runOnUiThread() 方法后返回一个值

xml - 如何为 XML 节点的无序列表创建具有出现约束的模式

python - 在解析大型 XML 文件时,我应该如何处理 Python 的 lxml 中的 XMLSyntaxError?

android - 如何在 FrameLayout 中排列元素?