0

我在这段代码中遇到了一个错误的异常

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 部分。有什么想法吗?

4

2 回答 2

0

我建议您进行以下更改:

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

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

然后将此“视图”与 findViewById() 一起使用,而不仅仅是 findViewById() 之类的

TextView tv = (TextView) view.findViewById(R.id.your_textView);
于 2013-08-16T09:48:56.120 回答
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);
于 2013-08-16T09:58:29.010 回答