【问题标题】:Finding the ID of a widget in a view class在视图类中查找小部件的 ID
【发布时间】:2012-06-10 15:52:31
【问题描述】:

我有一个几乎占据整个屏幕的画布,然后我想要一排按钮和一些其他小部件。所以我就这么做了。

XML Code
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="@+id/myLayout"
android:orientation="vertical"
android:layout_height="match_parent" >

<com.zone.manager.Tab3
    android:id="@+id/tab3_display"
    android:layout_width="fill_parent"
    android:layout_height="620dp" />

     <LinearLayout
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_height="match_parent" >   

        <Button
            android:id="@+id/addZone"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Add Zone" />

        <Button
            android:id="@+id/helpZone"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Help" />



        <SeekBar
            android:id="@+id/seekBar1"
            android:paddingTop="9dp"
            android:layout_width="179dp"
            android:layout_height="wrap_content" />

    </LinearLayout>

Java 代码

public class Tab3 extends View implements OnTouchListener, OnClickListener {
  public Tab3(Context context, AttributeSet attrs) {
    View parent = (View) getParent();
    addZone = (Button) parent.findViewById(R.id.addZone);
    addZone.setOnClickListener(this);
  }

    @Override
      protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        . . . draws a bunch of stuff
      }
    @Override
      public void onClick(View v) {
        switch (v.getId()) {
          case R.id.addZone:
            String w = "W~20~0~0~0~0~0";
            Log.d("ZoneSize", "Zone set");
            MyApplication.preferences.edit().putString( "ZoneSize", w ).commit();
            MyApplication.preferences.edit().putBoolean("ZoneSizeReady", true).commit();
            break;
        }
      }

但是我的问题是我认为代码没有重新协调 addZone 的位置。因为当我激活addZone.setOnClickListener 时,我的程序会崩溃,但是当我没有激活它时,布局看起来就像我想要的那样。我必须怎么做才能解决这个问题?

【问题讨论】:

    标签: java android xml view android-widget


    【解决方案1】:
     addZone = (Button) findViewById(R.id.addZone);
    

    addZone 将为 null,因为 com.zone.manager.Tab3 没有任何子节点

    所以很明显你的代码会崩溃

    因此,您要么给 com.zone.manager.Tab 孩子,这些孩子还需要将基类从 View 更改为 ViewGroup,

    或者您从 com.zone.manager.Tab 父级开始。类似的东西

     View parent = (View) getParent ();
     addZone = (Button) parent.findViewById(R.id.addZone);
    

    【讨论】:

    • 我需要做什么才能解决这个问题?
    • 当我这样做时它仍然在启动时崩溃?我会更新问题以向您展示我所拥有的。
    • 第二种方法不需要改变基类
    • 好的,我这样做了,但它仍然崩溃?我做错了什么。
    • 我没有看到更多问题。但是,使用调试器并检查值 parent 和 addZone。两者都不应 null
    【解决方案2】:

    我有一些技巧可以帮助您避免此类奇怪的错误和其他错误:

    自定义视图的代码依赖于使用自定义视图的 xml 布局。 这是糟糕的编码。

    相反,您应该使用 LayoutInflater ,为其自定义视图使用布局 xml 文件,然后执行“findViewById”并将 clickListeners 添加到您希望的任何视图中。

    我认为将自定义视图设置为保留其他视图的点击侦听器而不检查其中哪个被点击也是错误的。要么添加一个检查,要么为每个人添加一个不同的侦听器(我个人更喜欢)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-28
      相关资源
      最近更新 更多