在我的应用程序中,我想向用户展示一些DialogFragment
包含ListView
两个硬编码项目的 a。这些项目都是View
我编写的自定义类的实例,称为PictureMethodOptionItem
.
APictureMethodOptionItem
包含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
数小时的搜索只找到了我的代码中已经存在的解决方案(例如覆盖采用 a 的View
构造函数AttributeSet
)或不适用于我的案例 b/c 他们更多地指向活动及其setContentView()
方法。
有没有人看到我犯的错误(可能是非常无聊的错误)并能告诉我为什么 LayoutInflater 很难为我的小 DialogFragment 充气?