在我的应用程序中,我想向用户提供一个DialogFragment
,其中包含ListView
个带有两个硬编码的项目。这些项都是我编写的名为View
的自定义PictureMethodOptionItem
类的实例。
PictureMethodOptionItem
包含TextView
旁边的ImageView
。
picture_method_list.xml
:<ListView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:stereovise="http://schemas.android.com/apk/res/de.app.stereovise"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<de.app.stereovise.PictureMethodOptionItem
android:id="@+id/listItemGallery"
android:layout_width="match_parent"
android:layout_height="wrap_content"
stereovise:optionType="gallery" />
<de.app.stereovise.PictureMethodOptionItem
android:id="@+id/listItemCamera"
android:layout_width="match_parent"
android:layout_height="wrap_content"
stereovise:optionType="camera" />
</ListView>
picture_method_option.xml
:<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="7dp" >
<ImageView
android:id="@+id/method_option_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
android:layout_marginRight="10dp"
android:src="@drawable/output"
android:contentDescription="@string/methodOptionIcon" />
<TextView
android:id="@+id/method_option_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/method_option_icon"
android:layout_alignTop="@id/method_option_icon"
android:layout_centerVertical="true" />
</RelativeLayout>
最后但并非最不重要的是我的自定义View
类的Java代码:
PictureMethodOptionItem.java
:
public class PictureMethodOptionItem extends View {
private Context context;
private ImageView methodIcon;
private TextView methodText;
public PictureMethodOptionItem(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
initialize();
initFromAttributes(attrs);
}
public PictureMethodOptionItem(Context context) {
super(context);
this.context = context;
initialize();
}
private void initialize() {
LayoutInflater inflater = LayoutInflater.from(context);
inflater.inflate(R.layout.picture_method_option, null, false);
//View.inflate(context, R.layout.picture_method_option, null);
methodIcon = (ImageView) findViewById(R.id.method_option_icon);
methodText = (TextView) findViewById(R.id.method_option_text);
// at this point methodIcon and methodText are still null!
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
}
private void initFromAttributes(AttributeSet attrs) {
String type = attrs.getAttributeValue(
"http://schemas.android.com/apk/res/de.app.stereovise",
"optionType"
);
if("0".equals(type)) {
methodIcon.setImageResource(android.R.drawable.gallery_thumb);
methodText.setText(R.string.methodGallery);
} else {
methodIcon.setImageResource(android.R.drawable.ic_menu_camera);
methodText.setText(R.string.methodCamera);
}
}
}
DialogFragment
充气的相关代码来自PictureOptionsDialog.java
:
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
LayoutInflater inflater = LayoutInflater.from(context);
View contentView = inflater.inflate(R.layout.picture_method_list, null, false);
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder
.setTitle(R.string.pictureChoiceTitle)
.setCancelable(true)
.setView(contentView);
return builder.create();
}
问题是,如果我尝试show()
此对话框,它会给我以下错误:
10-09 23:34:46.259: E/AndroidRuntime(14660): FATAL EXCEPTION: main
10-09 23:34:46.259: E/AndroidRuntime(14660): android.view.InflateException: Binary XML file line #8: Error inflating class de.app.stereovise.PictureMethodOptionItem
10-09 23:34:46.259: E/AndroidRuntime(14660): at android.view.LayoutInflater.createView(LayoutInflater.java:513)
10-09 23:34:46.259: E/AndroidRuntime(14660): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:565)
10-09 23:34:46.259: E/AndroidRuntime(14660): at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
10-09 23:34:46.259: E/AndroidRuntime(14660): at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
10-09 23:34:46.259: E/AndroidRuntime(14660): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
10-09 23:34:46.259: E/AndroidRuntime(14660): at de.app.stereovise.PictureOptionsDialog.onCreateDialog(PictureOptionsDialog.java:22)
10-09 23:34:46.259: E/AndroidRuntime(14660): at android.support.v4.app.DialogFragment.getLayoutInflater(DialogFragment.java:295)
10-09 23:34:46.259: E/AndroidRuntime(14660): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:871)
10-09 23:34:46.259: E/AndroidRuntime(14660): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1083)
10-09 23:34:46.259: E/AndroidRuntime(14660): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:635)
10-09 23:34:46.259: E/AndroidRuntime(14660): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1431)
10-09 23:34:46.259: E/AndroidRuntime(14660): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:420)
10-09 23:34:46.259: E/AndroidRuntime(14660): at android.os.Handler.handleCallback(Handler.java:587)
10-09 23:34:46.259: E/AndroidRuntime(14660): at android.os.Handler.dispatchMessage(Handler.java:92)
10-09 23:34:46.259: E/AndroidRuntime(14660): at android.os.Looper.loop(Looper.java:144)
10-09 23:34:46.259: E/AndroidRuntime(14660): at android.app.ActivityThread.main(ActivityThread.java:4937)
10-09 23:34:46.259: E/AndroidRuntime(14660): at java.lang.reflect.Method.invokeNative(Native Method)
10-09 23:34:46.259: E/AndroidRuntime(14660): at java.lang.reflect.Method.invoke(Method.java:521)
10-09 23:34:46.259: E/AndroidRuntime(14660): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
10-09 23:34:46.259: E/AndroidRuntime(14660): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
10-09 23:34:46.259: E/AndroidRuntime(14660): at dalvik.system.NativeStart.main(Native Method)
10-09 23:34:46.259: E/AndroidRuntime(14660): Caused by: java.lang.reflect.InvocationTargetException
10-09 23:34:46.259: E/AndroidRuntime(14660): at de.app.stereovise.PictureMethodOptionItem.<init>(PictureMethodOptionItem.java:22)
10-09 23:34:46.259: E/AndroidRuntime(14660): at java.lang.reflect.Constructor.constructNative(Native Method)
10-09 23:34:46.259: E/AndroidRuntime(14660): at java.lang.reflect.Constructor.newInstance(Constructor.java:446)
10-09 23:34:46.259: E/AndroidRuntime(14660): at android.view.LayoutInflater.createView(LayoutInflater.java:500)
10-09 23:34:46.259: E/AndroidRuntime(14660): ... 20 more
10-09 23:34:46.259: E/AndroidRuntime(14660): Caused by: java.lang.NullPointerException
10-09 23:34:46.259: E/AndroidRuntime(14660): at de.app.stereovise.PictureMethodOptionItem.initFromAttributes(PictureMethodOptionItem.java:55)
10-09 23:34:46.259: E/AndroidRuntime(14660): ... 24 more
搜索时间只提出了我的代码中已经存在的解决方案(例如覆盖View
构造函数的AttributeSet
)或者不适用于我的情况b / c他们更多地指向活动及其setContentView()
方法。
有没有人看到我犯的错误(可能是非常沉闷的)并且可以告诉我为什么LayoutInflater难以让我的小DialogFragment膨胀?
答案 0 :(得分:6)
你称之为这个方法:
inflater.inflate(R.layout.picture_method_option, null, false);
你忽略了结果。最后一个参数false表示不将XML布局的内容添加到布局根目录(无论如何都设置为null)。您需要执行以下操作:
View v = inflater.inflate(R.layout.picture_method_option, null, false);
addView(v);
或者:
inflater.inflate(R.layout.picture_method_option, this, true);