2011-04-19 6 views
15

मैं एंड्रॉइड के लिए बहुत नया हूं और मैं अपने अनुकूलित डायलॉग प्रेफरेंस से मूल्यों को लोड/जारी रखने की कोशिश कर रहा हूं। वर्तमान में, यह विफल रहता है क्योंकि findViewById शून्य लौटाता है। क्या मैं इसे सही करने के लिए कोशिश करता हूं? कोड में मेरे संपादन टेक्स्ट विजेट तक पहुंच कैसे प्राप्त करूं?एक बढ़ते लेआउट के साथ एक कस्टम DialogPreference में विजेट का उपयोग कैसे करें?

public class AddressDialogPreference extends DialogPreference { 

public AddressDialogPreference(Context context, AttributeSet attrs) { 
    super(context, attrs); 

    setDialogLayoutResource(R.layout.address_dialog); 
} 

@Override 
protected void onBindDialogView(View view) { 

    EditText idField = (EditText) view.findViewById(R.id.hostID); 
    EditText ipField = (EditText) view.findViewById(R.id.hostIP); 

    SharedPreferences pref = getSharedPreferences(); 
    idField.setText(pref.getString(getKey() + "_id","ExampleHostname")); 
    ipField.setText(pref.getString(getKey() + "_ip","192.168.1.1")); 

    super.onBindDialogView(view); 
} 

@Override 
protected void onDialogClosed(boolean positiveResult) { 

    if(!positiveResult) 
     return; 

    Dialog myDial = getDialog(); 
    EditText idField = (EditText) myDial.findViewById(R.id.hostID); 
    EditText ipField = (EditText) myDial.findViewById(R.id.hostIP); 

    SharedPreferences.Editor editor = getEditor(); 
    editor.putString(getKey() + "_id",idField.getText().toString()); 
    editor.putString(getKey() + "_ip",ipField.getText().toString()); 
} 

address_dialog.xml:

<TextView 
    android:text="Insert IP address" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" /> 
<EditText 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/hostIP" /> 

<TextView 
    android:text="Insert identifier" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" /> 
<EditText 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/hostID" /> 

+0

आप ('hostID' की तरह) एक्सएमएल में आईडी के अंदर बड़े अक्षरों का उपयोग नहीं करना चाहिए

यहाँ लेआउट है –

उत्तर

21

ठीक है मैं इसे अपने आप में पता चला। खैर, मुझे अभी भी पता नहीं है कि त्रुटि का कारण क्या है, लेकिन मैंने लेआउट और कोड में बहुत सारे बदलाव किए और अचानक यह काम किया। मैंने यहां पोस्ट किए गए कोड पर वापस जाने की कोशिश की, लेकिन मैं त्रुटि को पुन: पेश नहीं कर सकता। मैं अपना कामकाजी कोड पोस्ट कर रहा हूं, इसलिए कोई भी जो इस समस्या में चलता है, इसका उपयोग कर सकता है।

एक व्यवस्थापक इस पोस्ट को हटाने का भी चयन कर सकता है, क्योंकि त्रुटि को पुन: उत्पन्न करना संभव नहीं हो सकता है।

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical"> 

<TextView 
    android:text="Insert IP address" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" /> 
<EditText 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/AddressBox" /> 

<TextView 
    android:text="Insert identifier" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" /> 
<EditText 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/HostnameBox" /> 
</LinearLayout> 

और AddressDialogPreference.java:

public class AddressDialogPreference extends DialogPreference { 

private EditText ipBox; 
private EditText hostBox; 

public AddressDialogPreference(Context context, AttributeSet attrs) { 
    super(context, attrs); 

    setDialogLayoutResource(R.layout.address_dialog); 
} 

@Override 
protected void onBindDialogView(View view) { 

    ipBox = (EditText) view.findViewById(R.id.AddressBox); 
    hostBox = (EditText) view.findViewById(R.id.HostnameBox); 

    SharedPreferences pref = getSharedPreferences(); 

    hostBox.setText(pref.getString(getKey() + "_host","ExampleHostname")); 
    ipBox.setText(pref.getString(getKey() + "_ip","192.168.1.1")); 

    super.onBindDialogView(view); 
} 

@Override 
protected void onDialogClosed(boolean positiveResult) { 

    if(!positiveResult) 
     return; 

    SharedPreferences.Editor editor = getEditor(); 
    editor.putString(getKey() + "_host",hostBox.getText().toString()); 
    editor.putString(getKey() + "_ip",ipBox.getText().toString()); 
    editor.commit(); 

    super.onDialogClosed(positiveResult); 
} 
}