为什么在添加linearlayout(Android)时会出现nullpointerexception?

时间:2012-04-12 14:54:37

标签: android layout canvas nullpointerexception

我正在尝试使用类扩展视图添加画布,然后将其添加到屏幕底部的线性布局中,在某些按钮下方。我知道我做得不对,但我无法弄清楚出了什么问题。当我尝试添加线性布局时,我得到了nullpointerexception。建议?

以下是代码的一部分:

public class Vectors extends Activity{

VectorsView vectorsView;
LinearLayout l;

public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    vectorsView = new VectorsView(this);
    l = (LinearLayout) findViewById(R.id.llCanvasV);
    l.addView(vectorsView); //Line 50
    setContentView(R.layout.vectors);
    .......
}

public class VectorsView extends View{

    public VectorsView(Context context) {
        super(context);
    }
    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.onDraw(canvas);       

        Paint paint = new Paint();
        paint.setColor(Color.WHITE);

        canvas.drawLine(0, 0, 100, 100, paint);
        vectorsView.draw(canvas);
        vectorsView.setLayoutParams(new LayoutParams(25, 25)); 
    }
}

}

这是xml的一部分:

<LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"
     android:background="@drawable/background" 
     android:orientation="vertical">

     <ImageView android:layout_height="wrap_content" 
        android:src="@drawable/vectors" 
        android:layout_width="wrap_content"
        android:layout_gravity="center"
        android:paddingTop="5dp"
        android:paddingBottom="5dp"
        android:adjustViewBounds="true">
     </ImageView>
     <LinearLayout android:layout_height="wrap_content" 
        android:layout_width="match_parent"
        android:orientation="horizontal">
        <Button android:text="Choose Program" 
            android:id="@+id/bChsProgV" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="97dp"
            android:adjustViewBounds="true">
        </Button>
        <ImageButton android:layout_height="wrap_content" 
            android:src="@drawable/help" 
            android:id="@+id/ibHelpV" 
            android:layout_width="wrap_content"
            android:layout_marginLeft="65dp"
            android:background="@null" 
            android:layout_marginTop="10dp">
        </ImageButton>
     </LinearLayout>
     <LinearLayout android:id="@+id/llCanvasV" 
        android:layout_height="wrap_content" 
        android:layout_width="match_parent">
     </LinearLayout>
</LinearLayout>

这是logcat:

 04-12 07:47:22.009: ERROR/AndroidRuntime(15072): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.prattia.webs.cheaterphysics/com.prattia.webs.cheaterphysics.Vectors}: java.lang.NullPointerException
04-12 07:47:22.009: ERROR/AndroidRuntime(15072):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
04-12 07:47:22.009: ERROR/AndroidRuntime(15072):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-12 07:47:22.009: ERROR/AndroidRuntime(15072):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-12 07:47:22.009: ERROR/AndroidRuntime(15072):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-12 07:47:22.009: ERROR/AndroidRuntime(15072):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-12 07:47:22.009: ERROR/AndroidRuntime(15072):     at android.os.Looper.loop(Looper.java:123)
04-12 07:47:22.009: ERROR/AndroidRuntime(15072):     at android.app.ActivityThread.main(ActivityThread.java:4627)
04-12 07:47:22.009: ERROR/AndroidRuntime(15072):     at java.lang.reflect.Method.invokeNative(Native Method)
04-12 07:47:22.009: ERROR/AndroidRuntime(15072):     at java.lang.reflect.Method.invoke(Method.java:521)
04-12 07:47:22.009: ERROR/AndroidRuntime(15072):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
04-12 07:47:22.009: ERROR/AndroidRuntime(15072):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
04-12 07:47:22.009: ERROR/AndroidRuntime(15072):     at dalvik.system.NativeStart.main(Native Method)
04-12 07:47:22.009: ERROR/AndroidRuntime(15072): Caused by: java.lang.NullPointerException
04-12 07:47:22.009: ERROR/AndroidRuntime(15072):     at com.prattia.webs.cheaterphysics.Vectors.onCreate(Vectors.java:50)
04-12 07:47:22.009: ERROR/AndroidRuntime(15072):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
04-12 07:47:22.009: ERROR/AndroidRuntime(15072):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)

3 个答案:

答案 0 :(得分:7)

在调用 findViewById 之前,您需要先调用 setContentView 。见下文:

public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.vectors);  //must be called before findViewById

    vectorsView = new VectorsView(this);
    l = (LinearLayout) findViewById(R.id.llCanvasV);
    l.addView(vectorsView); //Line 50
}

答案 1 :(得分:1)

常见错误:

l = (LinearLayout) findViewById(R.id.llCanvasV);

在当前内容视图中搜索,但未设置。

更改;

l = (LinearLayout) findViewById(R.id.llCanvasV);
l.addView(vectorsView); //Line 50
setContentView(R.layout.vectors);

setContentView(R.layout.vectors);
l = (LinearLayout) findViewById(R.id.llCanvasV);
l.addView(vectorsView); //Line 50

答案 2 :(得分:1)

你没有在第50行之前调用setContentView,因此第49行还没有找到LinearLayout。