简体   繁体   English

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

[英]Why am I getting a nullpointerexception when adding a linearlayout (Android)?

I'm attempting to use a class extending view to add a canvas and then add it to a linear layout at the bottom of the screen, underneath some buttons. 我正在尝试使用类扩展视图添加画布,然后将其添加到屏幕底部的线性布局中,在某些按钮下方。 I know I'm not doing it right, but I can't figure out what is wrong. 我知道我做得不对,但我无法弄清楚出了什么问题。 I'm getting a nullpointerexception when I try to add the linear layout. 当我尝试添加线性布局时,我得到了nullpointerexception。 Advice? 建议吗?

Here is part of the code: 以下是代码的一部分:

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)); 
    }
}

} }

And here is part of the xml: 这是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>

And here is the logcat: 这是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)

You need to call setContentView first before calling findViewById . 在调用findViewById之前,需要先调用setContentView See below: 见下文:

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
}

Common mistake: 常见的错误:

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

Searches in the current contentview, but that isn't set. 搜索当前内容视图,但未设置。

Change; 更改;

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

into

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我会收到 nullpointerexception (Android)? - Why am I getting a nullpointerexception (Android)? 为什么会出现NullPointerException? - Why am I getting NullPointerException? 尝试在Android中打开对话框时,为什么会出现NullPointerException? - Why am I getting NullPointerException when I try to open a dialog in Android? 为什么我为此线性布局获取空值 - Why am I getting a null value for this linearlayout 为什么我在尝试 setAdapter 时会收到 nullpointerexception? - Why am i getting a nullpointerexception when I try to setAdapter? Android - 为什么我在SQLiteOpenHelper onCreate中得到NullPointerException? - Android - Why am I getting NullPointerException in SQLiteOpenHelper onCreate? 为什么我从Android 7 Nougat收到NullPointerException? - Why am I getting a NullPointerException from Android 7 Nougat? 为什么我在Android 4.0.3中得到TimePicker NullPointerException? - Why am I getting TimePicker NullPointerException in android 4.0.3? 为什么我会得到一个nullpointerexception,可能是类错误(Android)? - Why am I getting a nullpointerexception, probably a class error (Android)? 为什么我在使用moondroid coverflow时会出现NullPointerException? - why am I getting NullPointerException when using moondroid coverflow?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM