2

我有一个由 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>
4

3 回答 3

3

它给你NullPointerException

解决方案是: - 写在下面的行

setContentView(R.layout.activity_main);

super.onCreate(savedInstanceState);

并写在下面的行

RelativeLayout ll = (RelativeLayout) findViewById(R.id.mRlayout1);

代替

RelativeLayout ll = (RelativeLayout) findViewById(R.layout.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:id="@+id/mRlayout1">

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

并从最后删除,它将解决您的问题。

于 2012-12-07T10:32:07.293 回答
1

您需要在调用setContentView之前调用findViewById.. 并在下面设置保证金检查..

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

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

layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,RelativeLayout.LayoutParams.FILL_PARENT);
layoutParams.setMargins(px_left, px_top, 0, 0);
btn.setLayoutParams(layoutParams);              
ll.addView(btn, layoutParams); // getting error here
于 2012-12-07T10:31:30.470 回答
0

尝试删除此行... btn.setLayoutParams(layoutParams);

当您在 ll.addView(btn, layoutParams); 中提供布局参数时

问候,\n 苏曼

于 2013-01-18T05:14:33.143 回答