Android NullPointerException指的是在xml中创建的View

时间:2015-01-15 13:13:38

标签: java android xml nullpointerexception android-framelayout

我有一个Android应用程序,它使用xml定义布局,然后以编程方式添加到它。当我尝试引用xml中定义的视图时,应用程序崩溃时出现nullpointerexception。

看起来该视图在xml中被引用为OK,因为该行没有产生任何问题,只有当我尝试访问视图时,才会引用它的属性或添加应用程序崩溃的子视图。 / p>

xml如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white">

    <RelativeLayout
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/container"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        tools:context=".MyActivity"
        tools:ignore="MergeRootFrame"

        <fragment android:name="uk.co.perfecthomecomputers.thebigsudoku.DokuSuperActivity$PlaceholderFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@+id/adFragment"
            tools:layout="@layout/fragment_blank" />

        <fragment
            android:id="@+id/adFragment"
            android:name="uk.co.perfecthomecomputers.thebigsudoku.DokuSuperActivity$AdFragment"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            tools:layout="@layout/fragment_ad" />
    </RelativeLayout>

</RelativeLayout>

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:id="@+id/frameLayout1">
</FrameLayout>

导致问题的代码是:

public class DokuSuperActivity extends Activity implements View.OnClickListener {

    View decorView;
    FrameLayout fl1, dokuHolder, dokuControls, dokuPen, dokuPencil, emailDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d("The Big Sudoku", "OnCreate");

        Bundle extras = getIntent().getExtras();
        dokuNumber = Integer.parseInt(extras.getString("DOKU_NUMBER"));
        setContentView(R.layout.activity_doku_super);
        decorView = getWindow().getDecorView();
        decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);
        try {
            getActionBar().hide();
        } catch (NullPointerException e) {
            Log.d("DOKU16", "NO ACTION BAR TO HIDE");
        }

        fl1 = (FrameLayout)findViewById(R.id.frameLayout1);  // No problem so far!
        FrameLayout.LayoutParams fl1params = new FrameLayout.LayoutParams(width, height);
        fl1.setLayoutParams(fl1params);  // Causes NullPointerException
        fl1.setBackgroundColor(getResources().getColor(R.color.clear));  // Causes NullPointerException

        iv1 = new ImageView(this);
        iv1.setLayoutParams(fl1params);
        iv1.setScaleType(ImageView.ScaleType.CENTER_CROP);

        fl1.addView(iv1);  // Causes NullPointerException
    }
}

错误日志如下:

    --------- beginning of crash
01-15 13:03:09.363    1074-1074/uk.co.perfecthomecomputers.thebigsudoku E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: uk.co.perfecthomecomputers.thebigsudoku, PID: 1074
    java.lang.RuntimeException: Unable to start activity ComponentInfo{uk.co.perfecthomecomputers.thebigsudoku/uk.co.perfecthomecomputers.thebigsudoku.DokuSuperActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setLayoutParams(android.view.ViewGroup$LayoutParams)' on a null object reference
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setLayoutParams(android.view.ViewGroup$LayoutParams)' on a null object reference
            at uk.co.perfecthomecomputers.thebigsudoku.DokuSuperActivity.onCreate(DokuSuperActivity.java:195)
            at android.app.Activity.performCreate(Activity.java:5933)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

我非常感谢所提供的任何建议。

由于

0 个答案:

没有答案