0

I've been searching how to add buttons dynamically and keep finding the same or very similar solution but it just refuses to work with me. Whenever I run the app it crashes instantly and I am unsure what I am doing wrong.

public class FindCalls extends Activity {
Button b;
LinearLayout layout;
LayoutParams lp;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_find_calls);
    initialize();

}

public void initialize() {
    layout = (LinearLayout) findViewById(R.layout.activity_find_calls);
    lp = new LayoutParams(LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT);
    b = new Button(this);

    layout.addView(b, lp);
    setContentView(layout);
    }
}

The activity_find_calls xml contains a LinearLayout.

LogCat

05-20 17:22:44.191: E/Trace(12959): error opening trace file: No such file or directory             (2)
05-20 17:22:44.449: E/AndroidRuntime(12959): FATAL EXCEPTION: main
05-20 17:22:44.449: E/AndroidRuntime(12959): java.lang.RuntimeException: Unable to                     start activity ComponentInfo{com.example.callfinder/com.example.callfinder.FindCalls}:     java.lang.NullPointerException
05-20 17:22:44.449: E/AndroidRuntime(12959):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2306)
05-20 17:22:44.449: E/AndroidRuntime(12959):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2356)
05-20 17:22:44.449: E/AndroidRuntime(12959):    at android.app.ActivityThread.access$600(ActivityThread.java:150)
05-20 17:22:44.449: E/AndroidRuntime(12959):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
05-20 17:22:44.449: E/AndroidRuntime(12959):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-20 17:22:44.449: E/AndroidRuntime(12959):    at android.os.Looper.loop(Looper.java:137)
05-20 17:22:44.449: E/AndroidRuntime(12959):    at android.app.ActivityThread.main(ActivityThread.java:5195)
05-20 17:22:44.449: E/AndroidRuntime(12959):    at java.lang.reflect.Method.invokeNative(Native Method)
05-20 17:22:44.449: E/AndroidRuntime(12959):    at java.lang.reflect.Method.invoke(Method.java:511)
05-20 17:22:44.449: E/AndroidRuntime(12959):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
05-20 17:22:44.449: E/AndroidRuntime(12959):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
05-20 17:22:44.449: E/AndroidRuntime(12959):    at dalvik.system.NativeStart.main(Native Method)
05-20 17:22:44.449: E/AndroidRuntime(12959): Caused by: java.lang.NullPointerException
05-20 17:22:44.449: E/AndroidRuntime(12959):    at com.example.callfinder.FindCalls.initialize(FindCalls.java:28)
05-20 17:22:44.449: E/AndroidRuntime(12959):    at com.example.callfinder.FindCalls.onCreate(FindCalls.java:18)
05-20 17:22:44.449: E/AndroidRuntime(12959):    at android.app.Activity.performCreate(Activity.java:5104)
05-20 17:22:44.449: E/AndroidRuntime(12959):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
05-20 17:22:44.449: E/AndroidRuntime(12959):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2260)
05-20 17:22:44.449: E/AndroidRuntime(12959):    ... 11 more

The app runs when I comment out the layout and setContentView so I have reason to believe that is what's causing the issue.

4

1 回答 1

1

findViewById(R.layout.activity_find_calls)

这是不正确的。R.layout引用 res/layout 中的 .xml 布局文件,findViewById()需要来自R.id. 因此,您可能会在null这里得到一个并使用它会导致NullPointerException.

setContentView(layout)

也不要这样做。layout假设您修复了上一个问题,您已经有一个内容视图,其中这是一个孩子。

于 2013-05-20T21:23:25.913 回答