【问题标题】:Android DialogFragment inner LinearLayout returning nullAndroid DialogFragment内部LinearLayout返回null
【发布时间】:2014-05-23 14:09:08
【问题描述】:

我目前正在开发一个需要自定义对话框片段的应用。该对话框由两个元素组成,一个消息和一个图像或一个圆形进度条视图。对于图像/进度条,我使用线性布局作为容器,我想保留对它的引用并在运行时动态添加图像/进度条视图。

问题:我可以找到对 textview 的引用,但是当我尝试获取对内部线性布局的引用时,它返回 null。我还尝试膨胀父布局并将其引用保留为 rootview,然后尝试 rootview.findViewById() 但没有成功。

奇怪:我很容易找到对内部 TextView 的引用,只有线性布局返回 null。

非常感谢有关此问题的一些指导,看起来很简单,但对于我的生活,我无法弄清楚为什么它返回 null。我已经发布了下面的代码。

package com.app.devicetest.dialogs;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.app.devicetest.R;

public class CustomDialog extends Dialog implements View.OnClickListener {

    public final static int STYLE_SUCCESS = 1;
    public final static int STYLE_FAILURE = 2;
    public final static int STYLE_PROGRESS = 3;

    private String message;
    private TextView dialogMessage;
    private LinearLayout dialogImageLayout;

//    private LinearLayout rootView;

    private Context mContext;

    public CustomDialog(Context context) {
        super(context);
        mContext = context;
    }

    public CustomDialog(Context context, int theme) {
        super(context, theme);
        mContext = context;
    }

    protected CustomDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        mContext = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

// NOTE: I have also tried using this method with rootView.findViewById but the inner linearlayout still returns null.
//        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//        rootView = (LinearLayout) inflater.inflate(R.layout.dialog_custom, null);

        setContentView(R.layout.dialog_custom);

        dialogMessage = (TextView) findViewById(R.id.dialogMessage);
        dialogMessage.setText(message);

        dialogImageLayout = (LinearLayout) findViewById(R.id.dialogImageLayout);

        setCancelable(true);
        setCanceledOnTouchOutside(true);
    }


    public void setDialogMessage(String msg) {
        message = msg;
    }

    public void dialogStyle(int style) {
        // Note: Always returns null here.
        Log.i("dialog", "dialog reference : " + ((dialogImageLayout == null) ? "null" : "not null"));


        if(style == STYLE_SUCCESS) {
        }
        else if(style == STYLE_FAILURE) {
        }
        else if(style == STYLE_PROGRESS) {

        }
    }
}

下面是布局文件:dialog_custom.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/custom_dialog_layout"
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/container_dropshadow">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:id="@+id/dialogImageLayout">

        </LinearLayout>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="22sp"
        android:id="@+id/dialogMessage" />

</LinearLayout>

调用对话框的代码:

    completeDialog = new CustomDialog(mContext, R.style.Theme_CustomDialog);
    completeDialog.dialogStyle(CustomDialog.STYLE_SUCCESS);
    completeDialog.setDialogMessage("Operation successfully completed.");
    completeDialog.show();

【问题讨论】:

  • 尝试扩展 DialogFragment 并覆盖 onCreateView()
  • 尴尬,你试过清理你的项目并重建它吗?
  • 我已经尝试过扩展 Dialog 并使用 onCreateView() 我在想当我获得参考时布局可能还没有膨胀。同样,这不太有意义,因为我能够获得对 TextView 的引用。还尝试了几次清理和重建:) 我正在使用 Nexus 7 来测试这段代码。

标签: android android-linearlayout android-dialogfragment


【解决方案1】:

我认为值得一提的是我是如何为同样遇到此错误的人克服此问题的。

经过大量研究,我找出了发生这种情况的原因。似乎在内部视图有机会膨胀之前调用了方法 dialogStyle()(可能是因为与 TextView 相比,膨胀视图需要更长的时间)。如果您在 dialogImageLayout 上的 onCreateView 上进行日志打印,您会注意到引用不是 NULL。

因此,当用户调用 dialogStyle() 并在对话框类中添加视图时,最好将引用变量保存到类中的当前样式,当您知道视图已正确膨胀并且您有对它的引用时。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-22
    • 1970-01-01
    • 2021-02-22
    • 1970-01-01
    • 2017-11-23
    • 2023-03-10
    • 1970-01-01
    • 2017-10-05
    相关资源
    最近更新 更多