我创建了一个扩展RelativeLayout的类,并在其中创建了一个方法,但是当我在MainActivity
中使用该方法时,app会在启动时停止。我搜索过,但没有找到具体的解决方案。
这是类代码:
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");
}
}
logcat:
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>
答案 0 :(得分:-1)
您已创建参数化构造函数,这就是您需要使用参数创建对象的原因。 尝试创建Case类的参数化对象。 使用以下代码
C00=new Case(this,attributeSet);
并致电您的方法。