Android版的LinearLayout空例外 [英] Android LinearLayout null exception
本文介绍了Android版的LinearLayout空例外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在 XML创建一个
我的 ID
的LinearLayout
但得到空指针异常。
我不明白了什么,因为我没有收到任何语法错误。
下面是我的的Java
code
私人OnClickListener的onClick(){
返回新OnClickListener(){
公共无效的onClick(视图v){
的System.out.println(布局1);
newBtnLayout();
的System.out.println(布局2);
的System.out.println(newBtnLayout());
nBtnLayoutList =(LinearLayout中)findViewById
(R.id.newBtnLayoutList);
的System.out.println(布局3);
nEntriesButton =(按钮)findViewById(R.id.newEntriesBtn);
的System.out.println(布局4);
的System.out.println(nBtnLayoutList);
nBtnLayoutList.addView(createBtnAddEntries());
的System.out.println(布局5);
}
};
}私人的LinearLayout newBtnLayout(){
的LinearLayout nBtnLayout =新的LinearLayout(本);
最终的LayoutParams lparams =新的LayoutParams
(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
nBtnLayout.setOrientation(LinearLayout.VERTICAL);
nBtnLayout.setLayoutParams(lparams);
nBtnLayout.setWeightSum(100);
nBtnLayout.setId(R.id.newBtnLayoutList);
返回nBtnLayout;
}
私人按钮createBtnAddEntries(){
的System.out.println(按钮1);
最终的LayoutParams lparams =新的LayoutParams
(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,30);
最终按钮addEntiresBtn =新按钮(本);
addEntiresBtn.setLayoutParams(lparams);
addEntiresBtn.setText(添加项);
addEntiresBtn.setId(R.id.newEntriesBtn);
返回addEntiresBtn;
}
这是的logcat
输出
10月11日至20日:02:02.295:我/的System.out(1077):布局0
10月11日至20日:02:02.305:我/的System.out(1077):1布局
10月11日至20日:02:02.305:我/的System.out(1077):布局2
10月11日至20日:02:02.305:我/的System.out(1077):android.widget.LinearLayout@412376c0
10月11日至20日:02:02.315:我/的System.out(1077):布局3
10月11日至20日:02:02.346:我/的System.out(1077):布局4
10月11日至20日:02:02.346:我/的System.out(1077):空
10月11日至20日:02:02.346:我/的System.out(1077):1按钮
10月11日至20日:02:02.356:我/的System.out(1077):按钮2
10月11日至20日:02:02.356:我/的System.out(1077):按钮3
10月11日至20日:02:02.365:我/的System.out(1077):按钮4
10月11日至20日:02:02.365:我/的System.out(1077):5按钮
10月11日至20日:02:02.365:我/的System.out(1077):按键6
10月11日至20日:02:02.365:D / AndroidRuntime(1077):关闭VM
10月11日至20日:02:02.365:W / dalvikvm(1077):主题ID = 1:螺纹未捕获的异常退出(组= 0x40a13300)
10月11日至20日:02:02.375:E / AndroidRuntime(1077):致命异常:主要
10月11日至20日:02:02.375:E / AndroidRuntime(1077):显示java.lang.NullPointerException
10月11日至20日:02:02.375:E / AndroidRuntime(1077):在com.daryl.jcu_project1_creditscoreform.Credit_score_form $ 1.onClick(Credit_score_form.java:66)
10月11日至20日:02:02.375:E / AndroidRuntime(1077):在android.view.View.performClick(View.java:4084)
10月11日至20日:02:02.375:E / AndroidRuntime(1077):在android.view.View $ PerformClick.run(View.java:16966)
10月11日至20日:02:02.375:E / AndroidRuntime(1077):在android.os.Handler.handleCallback(Handler.java:615)
10月11日至20日:02:02.375:E / AndroidRuntime(1077):在android.os.Handler.dispatchMessage(Handler.java:92)
10月11日至20日:02:02.375:E / AndroidRuntime(1077):在android.os.Looper.loop(Looper.java:137)
10月11日至20日:02:02.375:E / AndroidRuntime(1077):在android.app.ActivityThread.main(ActivityThread.java:4745)
10月11日至20日:02:02.375:E / AndroidRuntime(1077):在java.lang.reflect.Method.invokeNative(本机方法)
10月11日至20日:02:02.375:E / AndroidRuntime(1077):在java.lang.reflect.Method.invoke(Method.java:511)
10月11日至20日:02:02.375:E / AndroidRuntime(1077):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:786)
10月11日至20日:02:02.375:E / AndroidRuntime(1077):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10月11日至20日:02:02.375:E / AndroidRuntime(1077):在dalvik.system.NativeStart.main(本机方法)
10月11日至20日:07:02.445:I /流程(1077):发送信号。 PID:1077 SIG:9
解决方案
明显NullPointerException异常。请检查您 activity_credit_score_from.xml
实际上包含与该ID的布局。如果一切都正确,清理项目(项目=>清洁)。 Eclipse是一个可怕的IDE和爱的痛苦开发。
I created a id
in XML
for my LinearLayout
but getting null pointer exception.
I didn't understand what went as i not receiving any syntax error.
Here is my java
code
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;
}
This is the Logcat
output
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
解决方案
Obvious NullPointerException. Please check if your activity_credit_score_from.xml
actually contains a layout with that id. If everything's correct, clean your project (Project => Clean). Eclipse is a terrible IDE and loves suffering developers.
这篇关于Android版的LinearLayout空例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文