我有一個由XML設計的佈局。 然後,我想通過Java編程添加一個按鈕。 無論我如何使用marginparams,layoutparams,我在執行addview時總會遇到錯誤。Android - 動態添加按鈕 - 出錯
我在做什麼錯?什麼不見了?
此外,我收到了一個警告,即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>
請出示一些logcat的爲你得到 – Stupidus
喜Stupidus錯誤。它給出:NullPointerException。 –
用LogCat編輯我的問題 –