@ViewById不起作用

Pra*_*nna 2 android findviewbyid actionbarsherlock android-annotations

这是我的代码

活动:

@RoboGuice
@EActivity(R.layout.result_page)
public class ResultActivity extends SherlockListActivity implements ActionBar.OnNavigationListener{
    @ViewById TextView src;
    @ViewById TextView dest;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(src==null || dest==null)
            Toast.makeText(this, "@ViewById does not work", Toast.LENGTH_SHORT).show();

        //More Code here

    }
}
Run Code Online (Sandbox Code Playgroud)

布局的一部分: - 注意:有许多嵌套布局 - 仅显示布局文件的一部分

<RelativeLayout
                    android:id="@+id/topRight"
                    android:layout_toRightOf="@id/topLeft"
                    android:layout_height="wrap_content"
                    android:layout_width="0px"
                    android:layout_weight="0.8">
                <TextView
                        android:id="@+id/src"
                        android:layout_alignParentLeft="true"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textColor="#3399FF"
                        android:maxLines="1"
                        android:paddingLeft="8dp"
                        android:textSize="16dp"/>
</RelativeLayout>

<RelativeLayout
                    android:id="@+id/bottomRight"
                    android:layout_toRightOf="@id/bottomLeft"
                    android:layout_height="wrap_content"
                    android:layout_width="0px"
                    android:layout_weight="0.8">
                <TextView
                        android:id="@+id/dest"
                        android:layout_alignParentLeft="true"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:paddingLeft="8dp"
                        android:maxLines="1"
                        android:textColor="#3399FF"
                        android:paddingBottom="10dp"
                        android:textSize="16dp"/>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

我想在TextViews上做一个setText().我想知道我实现接口(我需要的)是否会使@ViewById成为故障.还是我错过了什么?

Joh*_*sen 7

Spikas的回复不正确,你在Android注释中使用@ViewById时不必使用findViewById.问题是onCreate尚未注入视图.尝试使用@AfterViews注释的方法访问它们,保证在注入视图后执行.

例如:

@AfterViews
void checkViews(){
if(src!=null && dest!=null)
        Toast.makeText(this, "@ViewById does work!", Toast.LENGTH_SHORT).show();
}
Run Code Online (Sandbox Code Playgroud)