2013-08-17 85 views
0

嗨朋友我在创建提醒框时遇到了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部分的问题。下面

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; 

    } 
} 

<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给出是否有任何解决方案,请?

+0

请给出一个解决方案............. – Jithin

+0

请CustomAutoCompleteTextView的确认包的名称是一样的XML编写的,在这它必须是com.example.netmdapp1.customViews – xmen

+0

发布整个堆栈跟踪。你的包名叫什么? – Vikram

回答

0

您需要使用以下方法。

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

你的包必须是相同的:com.example.netmdapp1.customViews

+0

在我的情况下,我不能直接使用getApplcationContext()。 – Jithin

+0

我可以知道getActivity()需要什么吗? –

+0

他正在使用片段 – xmen