创建自定义警报框时发生异常

吉廷

嗨,朋友,我在创建警报框时遇到了一个Android.View.InflateException。

private void createAlertBox(final AppointmentRow appointmentRow) 
{
    final Dialog dialog = new Dialog(myContext, R.style.Dialog);
    View layout = LayoutInflater.from(getActivity().getApplicationContext()).inflate(R.layout.custom_autocomplete, null);
.........}

问题出在行中

View layout = LayoutInflater.from(getActivity().getApplicationContext()).inflate(R.layout.custom_autocomplete, null);

这是错误日志。

 08-16 15:35:00.820: E/AndroidRuntime(3898): FATAL EXCEPTION: main
08-16 15:35:00.820: E/AndroidRuntime(3898): android.view.InflateException: Binary XML file line #36: Error inflating class com.example.netmdapp1.customViews.CustomAutoCompleteTextView
08-16 15:35:00.820: E/AndroidRuntime(3898):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:691)

这是xml部分出现的问题。

<com.example.netmdapp1.customViews.CustomAutoCompleteTextView
        android:id="@+id/customautocomplete"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:completionThreshold="1"
        android:ems="10"
        android:hint="Patient name"
        android:scrollbars="vertical"
        android:textColor="@android:color/black"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:background="@drawable/edittext_modified_states"
        android:layout_marginTop="5dp">
        <requestFocus />
    </com.example.netmdapp1.customViews.CustomAutoCompleteTextView>

CustomAutoCompleteTextView.java在下面给出

public class CustomAutoCompleteTextView extends AutoCompleteTextView {
    int mythreshold;
    HashMap<String, String> hm;
    PatientDetail patient;

    public CustomAutoCompleteTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        hm = new HashMap<String, String>();
        hm.put("id", "" + 0);

        // TODO Auto-generated constructor stub
    }

    @Override
    protected CharSequence convertSelectionToString(Object selectedItem) {
        hm = (HashMap<String, String>) selectedItem;
        return hm.get("name");
    }

    @Override
    public void setThreshold(int threshold) {
        int myThreshold = threshold;
        if (myThreshold < 0) {
            myThreshold = 0;
        }
        mythreshold = myThreshold;
    }

    @Override
    public boolean enoughToFilter() {
        return getText().length() >= mythreshold;
    }

    @Override
    public int getThreshold() {
        return mythreshold;
    }

    public String getid() {
        return hm.get("id");

    }

    public void setElement(HashMap<String, String> hm2) {
        hm = hm2;

    }
}

请问有什么解决办法吗?

哈迪克·乔希

您需要使用以下方式。

View viewToLoad = LayoutInflater.from(
                getActivity().getApplicationContext()).inflate(
                R.layout.custom_autocomplete, null);

您的包裹必须相同: com.example.netmdapp1.customViews

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章