gpt4 book ai didi

java - Android LinearLayout null 异常

转载 作者:行者123 更新时间:2023-12-01 15:02:16 27 4
gpt4 key购买 nike

我在 XML 中为我的 LinearLayout 创建了一个 id,但出现了空指针异常。我不明白发生了什么,因为我没有收到任何语法错误。

这是我的java代码

private OnClickListener onClick() {
return new OnClickListener() {
public void onClick(View v) {
System.out.println("layout 1");
newBtnLayout();
System.out.println("layout 2");
System.out.println(newBtnLayout());
nBtnLayoutList = (LinearLayout)findViewById
(R.id.newBtnLayoutList);
System.out.println("layout 3");
nEntriesButton = (Button) findViewById(R.id.newEntriesBtn);
System.out.println("layout 4");
System.out.println(nBtnLayoutList);
nBtnLayoutList.addView(createBtnAddEntries());
System.out.println("layout 5");
}
};
}

private LinearLayout newBtnLayout(){
LinearLayout nBtnLayout = new LinearLayout(this);
final LayoutParams lparams = new LayoutParams
(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
nBtnLayout.setOrientation(LinearLayout.VERTICAL);
nBtnLayout.setLayoutParams(lparams);
nBtnLayout.setWeightSum(100);
nBtnLayout.setId(R.id.newBtnLayoutList);
return nBtnLayout;
}


private Button createBtnAddEntries(){
System.out.println("button 1");
final LayoutParams lparams = new LayoutParams
(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT, 30);
final Button addEntiresBtn = new Button(this);
addEntiresBtn.setLayoutParams(lparams);
addEntiresBtn.setText("Add Entries");
addEntiresBtn.setId(R.id.newEntriesBtn);
return addEntiresBtn;
}

这是 Logcat 输出

11-20 10:02:02.295: I/System.out(1077): layout 0    
11-20 10:02:02.305: I/System.out(1077): layout 1
11-20 10:02:02.305: I/System.out(1077): layout 2
11-20 10:02:02.305: I/System.out(1077): android.widget.LinearLayout@412376c0
11-20 10:02:02.315: I/System.out(1077): layout 3
11-20 10:02:02.346: I/System.out(1077): layout 4
11-20 10:02:02.346: I/System.out(1077): null
11-20 10:02:02.346: I/System.out(1077): button 1
11-20 10:02:02.356: I/System.out(1077): button 2
11-20 10:02:02.356: I/System.out(1077): button 3
11-20 10:02:02.365: I/System.out(1077): button 4
11-20 10:02:02.365: I/System.out(1077): button 5
11-20 10:02:02.365: I/System.out(1077): button 6
11-20 10:02:02.365: D/AndroidRuntime(1077): Shutting down VM
11-20 10:02:02.365: W/dalvikvm(1077): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
11-20 10:02:02.375: E/AndroidRuntime(1077): FATAL EXCEPTION: main
11-20 10:02:02.375: E/AndroidRuntime(1077): java.lang.NullPointerException
11-20 10:02:02.375: E/AndroidRuntime(1077): at com.daryl.jcu_project1_creditscoreform.Credit_score_form$1.onClick(Credit_score_form.java:66)
11-20 10:02:02.375: E/AndroidRuntime(1077): at android.view.View.performClick(View.java:4084)
11-20 10:02:02.375: E/AndroidRuntime(1077): at android.view.View$PerformClick.run(View.java:16966)
11-20 10:02:02.375: E/AndroidRuntime(1077): at android.os.Handler.handleCallback(Handler.java:615)
11-20 10:02:02.375: E/AndroidRuntime(1077): at android.os.Handler.dispatchMessage(Handler.java:92)
11-20 10:02:02.375: E/AndroidRuntime(1077): at android.os.Looper.loop(Looper.java:137)
11-20 10:02:02.375: E/AndroidRuntime(1077): at android.app.ActivityThread.main(ActivityThread.java:4745)
11-20 10:02:02.375: E/AndroidRuntime(1077): at java.lang.reflect.Method.invokeNative(Native Method)
11-20 10:02:02.375: E/AndroidRuntime(1077): at java.lang.reflect.Method.invoke(Method.java:511)
11-20 10:02:02.375: E/AndroidRuntime(1077): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-20 10:02:02.375: E/AndroidRuntime(1077): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-20 10:02:02.375: E/AndroidRuntime(1077): at dalvik.system.NativeStart.main(Native Method)
11-20 10:07:02.445: I/Process(1077): Sending signal. PID: 1077 SIG: 9

最佳答案

明显的 NullPointerException。请检查您的 activity_credit_score_from.xml 是否确实包含具有该 ID 的布局。如果一切正确,请清理您的项目(Project => Clean)。 Eclipse 是一个糟糕的 IDE,它喜欢受苦受难的开发人员。

关于java - Android LinearLayout null 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13471031/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com