java - findViewByID() 在自定义 View 类中不断返回 null

标签 java android android-layout

在我的应用程序中,我想向用户展示一个小的 DialogFragment,其中包含一个带有两个硬编码项的 ListView。这些项目都是我编写的名为 PictureMethodOptionItem 的自定义 View 类的实例。

PictureMethodOptionItem 包含一个 TextView 和一个 ImageView

  • picture_method_list.xml:
<ListView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:stereovise="http://schemas.android.com/apk/res/de.app.stereovise"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <de.app.stereovise.PictureMethodOptionItem
        android:id="@+id/listItemGallery"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        stereovise:optionType="gallery" />

    <de.app.stereovise.PictureMethodOptionItem
        android:id="@+id/listItemCamera"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        stereovise:optionType="camera" />

</ListView>
  • picture_method_option.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="7dp" >

    <ImageView
        android:id="@+id/method_option_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_centerInParent="true"
        android:layout_marginRight="10dp"
        android:src="@drawable/output"
        android:contentDescription="@string/methodOptionIcon" />

    <TextView
        android:id="@+id/method_option_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/method_option_icon"
        android:layout_alignTop="@id/method_option_icon"
        android:layout_centerVertical="true" />

</RelativeLayout>

最后但同样重要的是我的自定义 View 类的 Java 代码:

PictureMethodOptionItem.java:

public class PictureMethodOptionItem extends View {

private Context context;

private ImageView methodIcon;
private TextView methodText;

public PictureMethodOptionItem(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.context = context;

    initialize();
    initFromAttributes(attrs);
}

public PictureMethodOptionItem(Context context) {
    super(context);
    this.context = context;

    initialize();
}

private void initialize() {
    LayoutInflater inflater = LayoutInflater.from(context);
    inflater.inflate(R.layout.picture_method_option, null, false);
    //View.inflate(context, R.layout.picture_method_option, null);

    methodIcon = (ImageView) findViewById(R.id.method_option_icon);
    methodText = (TextView) findViewById(R.id.method_option_text);
    // at this point methodIcon and methodText are still null!
}

@Override
protected void onFinishInflate() {
    super.onFinishInflate();


}

private void initFromAttributes(AttributeSet attrs) {
    String type = attrs.getAttributeValue(
        "http://schemas.android.com/apk/res/de.app.stereovise",
        "optionType"
    );

    if("0".equals(type)) {
        methodIcon.setImageResource(android.R.drawable.gallery_thumb);
        methodText.setText(R.string.methodGallery);
    } else {
        methodIcon.setImageResource(android.R.drawable.ic_menu_camera);
        methodText.setText(R.string.methodCamera);
    }
}

}

DialogFragment 膨胀的相关代码来自 PictureOptionsDialog.java:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    LayoutInflater inflater = LayoutInflater.from(context);
    View contentView = inflater.inflate(R.layout.picture_method_list, null, false);

    AlertDialog.Builder builder = new AlertDialog.Builder(context);

    builder
        .setTitle(R.string.pictureChoiceTitle)
        .setCancelable(true)
        .setView(contentView);

    return builder.create();
}

现在的问题是,如果我尝试 show() 这个对话框,它会给我以下错误:

10-09 23:34:46.259: E/AndroidRuntime(14660): FATAL EXCEPTION: main
10-09 23:34:46.259: E/AndroidRuntime(14660): android.view.InflateException: Binary XML file line #8: Error inflating class de.app.stereovise.PictureMethodOptionItem
10-09 23:34:46.259: E/AndroidRuntime(14660):    at android.view.LayoutInflater.createView(LayoutInflater.java:513)
10-09 23:34:46.259: E/AndroidRuntime(14660):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:565)
10-09 23:34:46.259: E/AndroidRuntime(14660):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
10-09 23:34:46.259: E/AndroidRuntime(14660):    at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
10-09 23:34:46.259: E/AndroidRuntime(14660):    at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
10-09 23:34:46.259: E/AndroidRuntime(14660):    at de.app.stereovise.PictureOptionsDialog.onCreateDialog(PictureOptionsDialog.java:22)
10-09 23:34:46.259: E/AndroidRuntime(14660):    at android.support.v4.app.DialogFragment.getLayoutInflater(DialogFragment.java:295)
10-09 23:34:46.259: E/AndroidRuntime(14660):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:871)
10-09 23:34:46.259: E/AndroidRuntime(14660):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1083)
10-09 23:34:46.259: E/AndroidRuntime(14660):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:635)
10-09 23:34:46.259: E/AndroidRuntime(14660):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1431)
10-09 23:34:46.259: E/AndroidRuntime(14660):    at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:420)
10-09 23:34:46.259: E/AndroidRuntime(14660):    at android.os.Handler.handleCallback(Handler.java:587)
10-09 23:34:46.259: E/AndroidRuntime(14660):    at android.os.Handler.dispatchMessage(Handler.java:92)
10-09 23:34:46.259: E/AndroidRuntime(14660):    at android.os.Looper.loop(Looper.java:144)
10-09 23:34:46.259: E/AndroidRuntime(14660):    at android.app.ActivityThread.main(ActivityThread.java:4937)
10-09 23:34:46.259: E/AndroidRuntime(14660):    at java.lang.reflect.Method.invokeNative(Native Method)
10-09 23:34:46.259: E/AndroidRuntime(14660):    at java.lang.reflect.Method.invoke(Method.java:521)
10-09 23:34:46.259: E/AndroidRuntime(14660):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
10-09 23:34:46.259: E/AndroidRuntime(14660):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
10-09 23:34:46.259: E/AndroidRuntime(14660):    at dalvik.system.NativeStart.main(Native Method)
10-09 23:34:46.259: E/AndroidRuntime(14660): Caused by: java.lang.reflect.InvocationTargetException
10-09 23:34:46.259: E/AndroidRuntime(14660):    at de.app.stereovise.PictureMethodOptionItem.<init>(PictureMethodOptionItem.java:22)
10-09 23:34:46.259: E/AndroidRuntime(14660):    at java.lang.reflect.Constructor.constructNative(Native Method)
10-09 23:34:46.259: E/AndroidRuntime(14660):    at java.lang.reflect.Constructor.newInstance(Constructor.java:446)
10-09 23:34:46.259: E/AndroidRuntime(14660):    at android.view.LayoutInflater.createView(LayoutInflater.java:500)
10-09 23:34:46.259: E/AndroidRuntime(14660):    ... 20 more
10-09 23:34:46.259: E/AndroidRuntime(14660): Caused by: java.lang.NullPointerException
10-09 23:34:46.259: E/AndroidRuntime(14660):    at de.app.stereovise.PictureMethodOptionItem.initFromAttributes(PictureMethodOptionItem.java:55)
10-09 23:34:46.259: E/AndroidRuntime(14660):    ... 24 more

数小时的搜索只提出了我的代码中已经存在的解决方案(例如覆盖采用 AttributeSetView 构造函数)或不适用于我的案例 b/c 他们更多地指向 Activities 和他们的 setContentView() 方法。

有没有人看到我犯的错误(可能是非常无聊的错误)并且可以告诉我为什么 LayoutInflater 很难膨胀我的小 DialogFragment?

最佳答案

你调用这个方法:

inflater.inflate(R.layout.picture_method_option, null, false);

而你忽略了结果。最后一个参数 false 表示将 XML 布局的内容添加到布局根(无论如何设置为 null。)您需要执行如下操作:

View v = inflater.inflate(R.layout.picture_method_option, null, false);
addView(v);

或者:

inflater.inflate(R.layout.picture_method_option, this, true);

关于java - findViewByID() 在自定义 View 类中不断返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12808858/

相关文章:

java - ConcurrentHashMap 中的 Dose Segment 存在虚假共享问题?

javascript - OJET 网格不显示

android - 如何在没有 gradle 的情况下使用 androidx 库

Android - 如果没有互联网连接则显示一条消息并继续检查

Android 布局 - 不显示 ImageView

java - Persistance 的 CriteriaBuilder - 检查是否大于 0

带有竖线的 Java 正则表达式

Android AlarmManager 设置功能不起作用?

android - 无法从 svn checkout

android - Appcompat Alert Dialog Action 按钮背景 On pressed state