2

我正在尝试使用类扩展视图来添加画布,然后将其添加到屏幕底部的线性布局中,在一些按钮下方。我知道我做得不对,但我不知道哪里出了问题。尝试添加线性布局时出现空指针异常。建议?

以下是部分代码:

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)
4

3 回答 3

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
}
于 2012-04-12T14:56:13.860 回答
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
于 2012-04-12T14:57:11.457 回答
1

您尚未在第 50 行之前调用 setContentView,因此第 49 行还找不到 LinearLayout。

于 2012-04-12T14:57:31.147 回答