Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:一个findViewById()方法,返回我们不知道的值';我不需要投_Android_Findviewbyid - Fatal编程技术网

Android:一个findViewById()方法,返回我们不知道的值';我不需要投

Android:一个findViewById()方法,返回我们不知道的值';我不需要投,android,findviewbyid,Android,Findviewbyid,由于我厌倦了为每个返回原始视图的Activity.findViewById()编写cast操作符,我终于尝试了 公共抽象类MyActivity扩展活动{ @抑制警告(“未选中”) 受保护的T findViewByID(内部id){ 返回(T)this.findViewById(id); } } 注意这不是重载(最后一个“D”是大写)。编译器说我们不能将视图转换为t。我的电脑有什么问题吗?奇怪的是,这一建议在英文网站上几乎看不到(例如,即使在我们可爱的堆栈溢出中),只有上面发布的网站例外 这在我

由于我厌倦了为每个返回原始
视图的
Activity.findViewById()
编写cast操作符,我终于尝试了

公共抽象类MyActivity扩展活动{
@抑制警告(“未选中”)
受保护的T findViewByID(内部id){
返回(T)this.findViewById(id);
}
}

注意这不是重载(最后一个“D”是大写)。编译器说我们不能将
视图
转换为
t
。我的电脑有什么问题吗?奇怪的是,这一建议在英文网站上几乎看不到(例如,即使在我们可爱的堆栈溢出中),只有上面发布的网站例外

这在我的测试项目中运行良好。没有编译器错误:

老实说,这种方法增加了一些微妙的复杂性开销,让下一个Android维护人员(将使用强制转换方法)在代码文件中保存一些字符


我建议以传统的方式强制转换视图,或者选择基于反射的解决方案,如。

我通过使用自定义eclipse builder为每个布局文件生成带有引用的类来解决这个问题,因为:

  • 它的类型安全,易于使用
  • RoboGuice和所有其他基于反射的API在Android上运行速度非常慢
在我看来,这是解决这个问题最干净、最有效的方法

请参见我的要点:

布局:test.xml

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android" >

    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/text2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/text1" />

    <ScrollView
        android:id="@+id/scroll"
        android:layout_width="match_parent"
        android:layout_height="350px"
        android:layout_below="@id/text2"
        android:layout_marginTop="50px" />

</merge>
生成的文件(在
gen/
中):ViewRef\u test.java

public final class TestView extends RelativeLayout {

    //~ Constructors ---------------------------------------------------------------------------------------------------
    private final ViewRef_test v;

    public TestView(final Context context) {
        super(context);

        LayoutInflater.from(context).inflate(R.layout.test, this, true);
        this.v = ViewRef_test.create(this);

        this.v.text1.setText();
        this.v.scroll.doSomething();
    }
}
package org.somecompany.somepackage;

import android.view.*;
import android.widget.*;
import java.lang.String;


@SuppressWarnings("unused")
public final class ViewRef_test {

    public final TextView text1;
    public final TextView text2;
    public final ScrollView scroll;


    private ViewRef_test(View root) {
        this.text1 = (TextView) root.findViewById(R.id.text1);
        this.text2 = (TextView) root.findViewById(R.id.text2);
        this.scroll = (ScrollView) root.findViewById(R.id.scroll);
    }

    public static ViewRef_test create(View root) {
        return new ViewRef_test(root);
    }


}

你的JDK和android版本是什么?我的是android 2.2和JDK 1.6。你的JDK和android版本是什么?我正要发布同样的东西-@Quv你还在项目的其他地方使用Activity.findviewbyd()吗?也许可以用一个更明显的方法名称来区分这两个…JDK 1.6.030,Android目标API-16Mine是Android 2.2和JDK 1.6。刚刚切换到API-8(2.2),也没问题。谢谢。我的小项目背后只有一个人,维护者是我,但是你的介绍似乎很有趣。
package org.somecompany.somepackage;

import android.view.*;
import android.widget.*;
import java.lang.String;


@SuppressWarnings("unused")
public final class ViewRef_test {

    public final TextView text1;
    public final TextView text2;
    public final ScrollView scroll;


    private ViewRef_test(View root) {
        this.text1 = (TextView) root.findViewById(R.id.text1);
        this.text2 = (TextView) root.findViewById(R.id.text2);
        this.scroll = (ScrollView) root.findViewById(R.id.scroll);
    }

    public static ViewRef_test create(View root) {
        return new ViewRef_test(root);
    }


}