InflateException创建对话框时

时间:2013-08-16 09:37:05

标签: android

我在此代码中遇到了一个infalte异常

public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
appContext=getActivity().getApplicationContext();
myContext=getActivity();
}

private void createAlertBox(final AppointmentRow appointmentRow)
{
final Dialog dialog = new Dialog(myContext, R.style.Dialog);
View layout = LayoutInflater.from(appContext).inflate(R.layout.custom_autocomplete, null);
String[] from={"id","name"};
.....................
.....................
}

以下行发生了异常。

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

异常是Android.View.InflateException。 有没有解决方案?

    <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>
</LinearLayout>
</LinearLayout>

这是Xml部分。任何想法?

2 个答案:

答案 0 :(得分:0)

我建议你做出以下改变:

LayoutInflater layoutInflater = (LayoutInflater) your_Context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

View view = layoutInflater.inflate(R.layout.custom_autocomplete, null); 

然后将此'view'与findViewById()一起使用,而不仅仅是像findViewById()

TextView tv = (TextView) view.findViewById(R.id.your_textView);

答案 1 :(得分:0)

将此代码更改为

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

                     to

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

                     or

LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.custom_autocomplete, null);