尝试动态地向布局添加按钮时崩溃

时间:2013-05-20 21:15:47

标签: android dynamic android-linearlayout

我一直在寻找如何动态添加按钮并继续寻找相同或非常相似的解决方案,但它只是拒绝与我合作。每当我运行应用程序时,它立即崩溃,我不确定我做错了什么。

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

activity_find_calls xml包含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

应用程序在我注释掉布局和setContentView时运行,所以我有理由相信这就是导致问题的原因。

1 个答案:

答案 0 :(得分:1)

  

findViewById(R.layout.activity_find_calls)

这不正确。 R.layout引用res / layout中的.xml布局文件,findViewById()需要R.id的标识符。因此,您可能会在此处获得null,并且使用它会导致NullPointerException

  

setContentView(layout)

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