Java 使用已创建类的方法-NullPointerException

Java 使用已创建类的方法-NullPointerException,java,android,android-layout,android-custom-view,Java,Android,Android Layout,Android Custom View,我创建了一个扩展RelativeLayout的类,并在其中创建了一个方法,但是当我在我的MainActivity中使用该方法时,应用程序在启动时停止。我已经搜索过了,但没有找到具体的解决方案 这是类代码: public class Case extends RelativeLayout { private TextView ta; private TextView tb; private TextView tc; private TextView td;

我创建了一个扩展RelativeLayout的类,并在其中创建了一个方法,但是当我在我的
MainActivity
中使用该方法时,应用程序在启动时停止。我已经搜索过了,但没有找到具体的解决方案

这是类代码:

public class Case extends RelativeLayout {
    private TextView ta;
    private TextView tb;
    private TextView tc;
    private TextView td;
    public Case(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        init();
    }

    public Case(Context context, AttributeSet attrs) {
        super(context);
        // TODO Auto-generated constructor stub
        init();
    }

    private void init() {
        // TODO Auto-generated method stub
        int wrap = LayoutParams.WRAP_CONTENT;
        LayoutParams top = new RelativeLayout.LayoutParams(wrap, wrap);
        LayoutParams right = new RelativeLayout.LayoutParams(wrap, wrap);
        LayoutParams bottom = new RelativeLayout.LayoutParams(wrap, wrap);
        LayoutParams left = new RelativeLayout.LayoutParams(wrap, wrap);
        top.addRule(RelativeLayout.CENTER_HORIZONTAL);
        top.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        right.addRule(RelativeLayout.CENTER_VERTICAL);
        right.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        bottom.addRule(RelativeLayout.CENTER_HORIZONTAL);
        bottom.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        left.addRule(RelativeLayout.CENTER_VERTICAL);
        left.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        ta = new TextView(getContext());
        tb = new TextView(getContext());
        tc = new TextView(getContext());
        td = new TextView(getContext());
        ta.setText("a");
        tb.setText("b");
        tc.setText("c");
        td.setText("d");
        this.addView(ta,top);
        this.addView(tb,right);
        this.addView(tc,bottom);
        this.addView(td,left);
    }

     void setNumbers(String a, String b, String c, String d){
        ta.setText(a);
        tb.setText(b);
        tc.setText(c);
        td.setText(d);
    }
}
这是主要代码:

public class Main extends Activity {
    Case c00,c01;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        c00 = (Case)findViewById(R.id.case1);
        c00.setNumbers("5","8","6","2");
    }
}
日志:

11-19 18:43:38.598: E/AndroidRuntime(16735): FATAL EXCEPTION: main
11-19 18:43:38.598: E/AndroidRuntime(16735): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.burawi.u5ra/com.burawi.u5ra.Main}: java.lang.NullPointerException
11-19 18:43:38.598: E/AndroidRuntime(16735):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
11-19 18:43:38.598: E/AndroidRuntime(16735):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
11-19 18:43:38.598: E/AndroidRuntime(16735):    at android.app.ActivityThread.access$600(ActivityThread.java:128)
11-19 18:43:38.598: E/AndroidRuntime(16735):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
11-19 18:43:38.598: E/AndroidRuntime(16735):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-19 18:43:38.598: E/AndroidRuntime(16735):    at android.os.Looper.loop(Looper.java:137)
11-19 18:43:38.598: E/AndroidRuntime(16735):    at android.app.ActivityThread.main(ActivityThread.java:4514)
11-19 18:43:38.598: E/AndroidRuntime(16735):    at java.lang.reflect.Method.invokeNative(Native Method)
11-19 18:43:38.598: E/AndroidRuntime(16735):    at java.lang.reflect.Method.invoke(Method.java:511)
11-19 18:43:38.598: E/AndroidRuntime(16735):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
11-19 18:43:38.598: E/AndroidRuntime(16735):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
11-19 18:43:38.598: E/AndroidRuntime(16735):    at dalvik.system.NativeStart.main(Native Method)
11-19 18:43:38.598: E/AndroidRuntime(16735): Caused by: java.lang.NullPointerException
11-19 18:43:38.598: E/AndroidRuntime(16735):    at com.burawi.u5ra.Main.onCreate(Main.java:15)
11-19 18:43:38.598: E/AndroidRuntime(16735):    at android.app.Activity.performCreate(Activity.java:4465)
11-19 18:43:38.598: E/AndroidRuntime(16735):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
11-19 18:43:38.598: E/AndroidRuntime(16735):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
11-19 18:43:38.598: E/AndroidRuntime(16735):    ... 11 more
main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Main">

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="wrap_content"
        android:layout_height="50dp" >

        <com.burawi.u5ra.Case
            android:id="@+id/case1"
            android:layout_width="50dp"
            android:layout_height="wrap_content" >

        </com.burawi.u5ra.Case>

        <com.burawi.u5ra.Case
            android:id="@+id/case2"
            android:layout_width="50dp"
            android:layout_height="wrap_content" >
        </com.burawi.u5ra.Case>

    </TableRow>



</LinearLayout>

您已经创建了参数化构造函数,这就是为什么您需要创建带有参数的对象。 尝试创建Case类的参数化对象。 使用以下代码

C00=新案例(此,属性集)


并调用您的方法。

请发布您的LogCat错误…完成了!我加了一句,你也可以分享你的main.xml布局吗?从外观上看,变量c00是空的,那么它是否包含在布局中?是的,它包含在布局中!我添加了main.xml我希望它影响我在布局“main”中放置的视图