我有一个由 XML 设计的布局。然后,我想通过Java编程添加一个按钮。无论我如何使用marginparams、layoutparams,我在执行addview 时总是会出错。
我究竟做错了什么?什么不见了?
另外,我收到警告,MarginLayoutParams 中的 RelativeLayout.LayoutParams.FILL_PARENT 已被弃用。那么另一种方法是什么?我正在尝试将新按钮放在相对布局上的特定位置。
谢谢, AJ
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button btn = new Button(this);
btn.setText("Go!");
btn.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
number_of_cards=0;
}
});
RelativeLayout ll = (RelativeLayout) findViewById(R.layout.activity_main);
//ll.addView(btn);
//setContentView(ll);
float density = getApplicationContext().getResources().getDisplayMetrics().density;
MarginLayoutParams marginParams;
RelativeLayout.LayoutParams layoutParams;
int px_left = Math.round((float) (30+(4-1)*75) * density);
int px_top = Math.round((float) (30+(1-1)*91) * density);
marginParams = new MarginLayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,RelativeLayout.LayoutParams.FILL_PARENT);
marginParams.setMargins(px_left, px_top, 0, 0);
layoutParams = new RelativeLayout.LayoutParams(marginParams);
btn.setLayoutParams(layoutParams);
ll.addView(btn, layoutParams); // getting error here
setContentView(R.layout.activity_main);
LogCat 是:
12-07 10:57:03.534: E/AndroidRuntime(850): FATAL EXCEPTION: main
12-07 10:57:03.534: E/AndroidRuntime(850): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.first/com.example.first.First}: java.lang.NullPointerException
12-07 10:57:03.534: E/AndroidRuntime(850): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
12-07 10:57:03.534: E/AndroidRuntime(850): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
12-07 10:57:03.534: E/AndroidRuntime(850): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-07 10:57:03.534: E/AndroidRuntime(850): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
12-07 10:57:03.534: E/AndroidRuntime(850): at android.os.Handler.dispatchMessage(Handler.java:99)
12-07 10:57:03.534: E/AndroidRuntime(850): at android.os.Looper.loop(Looper.java:123)
12-07 10:57:03.534: E/AndroidRuntime(850): at android.app.ActivityThread.main(ActivityThread.java:3683)
12-07 10:57:03.534: E/AndroidRuntime(850): at java.lang.reflect.Method.invokeNative(Native Method)
12-07 10:57:03.534: E/AndroidRuntime(850): at java.lang.reflect.Method.invoke(Method.java:507)
12-07 10:57:03.534: E/AndroidRuntime(850): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-07 10:57:03.534: E/AndroidRuntime(850): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-07 10:57:03.534: E/AndroidRuntime(850): at dalvik.system.NativeStart.main(Native Method)
12-07 10:57:03.534: E/AndroidRuntime(850): Caused by: java.lang.NullPointerException
12-07 10:57:03.534: E/AndroidRuntime(850): at com.example.first.First.onCreate(First.java:127)
12-07 10:57:03.534: E/AndroidRuntime(850): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-07 10:57:03.534: E/AndroidRuntime(850): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
12-07 10:57:03.534: E/AndroidRuntime(850): ... 11 more
我的 activity_main xml 如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textScore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="30dp"
android:layout_marginTop="5dp"
android:text="@string/text_score" />
</RelativeLayout>