我有一个由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>
答案 0 :(得分: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>
从最后删除,它将解决您的问题。
答案 1 :(得分: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
答案 2 :(得分:0)
尝试删除此行... btn.setLayoutParams(的LayoutParams);
在提供布局参数时 ll.addView(btn,layoutParams);
的问候,\ n 苏曼