2013-08-17 72 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