当我尝试以编程方式向我的NullPointerException
添加夸大的观看次数时,我收到了ViewFlipper
:
- onCreate
setContentView(R.layout.event_report);
final LayoutInflater inflater = getLayoutInflater();
mFlipper = (ViewFlipper) findViewById(R.id.eventReportViewFlipper);
final View typeView = inflater
.inflate(R.layout.event_report_type, null)
mFlipper.addView(typeView, VIEW_TYPE, null); //NullPointerException
- event_report.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/darker_gray"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:color/background_dark"
android:gravity="center"
android:text="@string/eventReportCaption"
android:textAppearance="?android:attr/textAppearanceLarge"/>
<ViewFlipper
android:id="@+id/eventReportViewFlipper"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
- event_report_type.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/eventReportViewTypeUpperLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:id="@+id/eventReportViewTypeLowerLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
我计划稍后初始化typeView。
这是我的堆栈跟踪:
11-28 16:59:56.280: E/AndroidRuntime(8604): Caused by: java.lang.NullPointerException
11-28 16:59:56.280: E/AndroidRuntime(8604): at android.view.ViewGroup$LayoutParams.<init>(ViewGroup.java:3582)
11-28 16:59:56.280: E/AndroidRuntime(8604): at android.view.ViewGroup$MarginLayoutParams.<init>(ViewGroup.java:3732)
11-28 16:59:56.280: E/AndroidRuntime(8604): at android.widget.FrameLayout$LayoutParams.<init>(FrameLayout.java:492)
11-28 16:59:56.280: E/AndroidRuntime(8604): at android.widget.FrameLayout.generateLayoutParams(FrameLayout.java:436)
11-28 16:59:56.280: E/AndroidRuntime(8604): at android.view.ViewGroup.addViewInner(ViewGroup.java:1984)
11-28 16:59:56.280: E/AndroidRuntime(8604): at android.view.ViewGroup.addView(ViewGroup.java:1874)
11-28 16:59:56.280: E/AndroidRuntime(8604): at android.widget.ViewAnimator.addView(ViewAnimator.java:160)
11-28 16:59:56.280: E/AndroidRuntime(8604): at fi.emicode.flashpoint.EventDialog.onCreate(EventDialog.java:43)
答案 0 :(得分:2)
这是来自Android文档的addView方法
addView(View child, int index, ViewGroup.LayoutParams params)
是类型为int的VIEW_TYPE吗?
我认为您应该为添加的视图指定layoutparams或仅使用addView 2个参数
addView(VIEW_TYPE,index);