【问题标题】:How to find the FrameLayout in my class如何在我的课堂上找到 FrameLayout
【发布时间】:2014-06-06 20:33:47
【问题描述】:

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

我是我的班级,我想显示屏幕并隐藏另一个屏幕内的 FrameLayout,当用户单击按钮时,会出现 FrameLayout is hidden,但我收到错误消息。

是否可以通过findViewById在另一个内部找到这个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>

类:

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 

}

因为 cmets 而更新:

所以,我的日志显示:

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

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

这些代码在那里。

在我的“开始”类中,我创建了一个对象:

ShowInfo showObj = new ShowInfo(getContext());

在我的onCreateStart 中我打电话:

showObj.hideImages();

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

【问题讨论】:

  • 错误是什么?发布您的日志猫
  • 你在膨胀什么布局?发布 setContentView(R.layout.yourXML); 的代码
  • 在创建时发布 Start.java

标签: android xml android-framelayout


【解决方案1】:

由于您是在另一个类中工作,因此您需要传递视图的根元素才能使用findViewById()。否则,您的类将脱离上下文,无法访问视图元素。

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

【讨论】:

  • 我找到了解决方案:我可以在设置内容视图的同一个类中做所有事情。就在我创建另一个类来执行此操作时,我遇到了错误。我不知道为什么。
  • 哦,是的,我明白了。我会为此更新我的答案。
【解决方案2】:

您可以做的是扩充您的 XML 布局文件并使用 layout.findViewById(...) 找到您的框架

例子:

LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
FrameLayout layout = (FrameLayout) inflater.inflate(R.layout.yourfilename, null);
FrameLayout layoutteamInfo  = (FrameLayout) layout.findViewById(R.id.teamInfoLayout);

这将在您的 xml 文件中获得第二个框架布局

【讨论】:

  • 我这样做了,但我仍然收到错误消息。问题是函数 hideImges/
【解决方案3】:

你正在这样做:

waves = (ImageView) findViewById(R.id.imageViewwaves_startScreen);

但是您的 XML 中没有具有此 ID 的 ImageView:imageViewwaves_startScreen。 您必须使用该 ID 在 Xml 中创建一个 ImageView

尝试为您的第一个 FrameLayout 提供一个 id。

如果这不起作用而不是尝试将您为处理布局所做的此类复制到您的 Activity 类,如果这可行,则意味着 raybaybay 答案就是解决方案

【讨论】:

  • 其实我有,只是忘了放那里。我更新它。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-11
  • 1970-01-01
  • 2019-07-03
  • 1970-01-01
  • 2012-04-08
  • 1970-01-01
相关资源
最近更新 更多