java - Android - 空对象引用错误

标签 java android android-layout

这里有一些 Android 代码给我一个空对象引用错误。不知道为什么会这样,但是我发现这与 setStep() 方法有关。不确定是什么导致了这个错误,但这是我在 logcat 中得到的:

02-26 23:00:14.081  24821-24821/com.android.usagemonitor E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.android.usagemonitor, PID: 24821
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.usagemonitor/com.android.usemoncore.Activities.TypeActivities.NetActivity}: android.view.InflateException: Binary XML file line #7: Binary XML file line #7: Error inflating class com.android.usemoncore.Views.TypeLinearLayouts.NetLinearLayout
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3254)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3350)
        at android.app.ActivityThread.access$1100(ActivityThread.java:222)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1795)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:158)
        at android.app.ActivityThread.main(ActivityThread.java:7229)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
 Caused by: android.view.InflateException: Binary XML file line #7: Binary XML file line #7: Error inflating class com.android.usemoncore.Views.TypeLinearLayouts.NetLinearLayout
        at android.view.LayoutInflater.inflate(LayoutInflater.java:551)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:429)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:380)
        at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:474)
        at android.app.Activity.setContentView(Activity.java:2387)
        at com.android.usemoncore.Activities.TypeActivities.NetActivity.onCreate(NetActivity.java:12)
        at android.app.Activity.performCreate(Activity.java:6876)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3207)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3350)  
        at android.app.ActivityThread.access$1100(ActivityThread.java:222)     
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1795)                                   
        at android.os.Handler.dispatchMessage(Handler.java:102)                              
        at android.os.Looper.loop(Looper.java:158)                                        
        at android.app.ActivityThread.main(ActivityThread.java:7229)                                                       
        at java.lang.reflect.Method.invoke(Native Method)                                                               
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
 Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class com.android.usemoncore.Views.TypeLinearLayouts.NetLinearLayout
        at android.view.LayoutInflater.createView(LayoutInflater.java:657)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:776)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:716)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:847)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:810)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:855)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:810)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:527)                          
        at android.view.LayoutInflater.inflate(LayoutInflater.java:429)          
        at android.view.LayoutInflater.inflate(LayoutInflater.java:380)         
        at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:474)                                            
        at android.app.Activity.setContentView(Activity.java:2387)                             
        at com.android.usemoncore.Activities.TypeActivities.NetActivity.onCreate(NetActivity.java:12)                                                                                    
        at android.app.Activity.performCreate(Activity.java:6876)                      
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)                 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3207)                
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3350)                           
        at android.app.ActivityThread.access$1100(ActivityThread.java:222)  
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1795)                            
        at android.os.Handler.dispatchMessage(Handler.java:102)                                 
        at android.os.Looper.loop(Looper.java:158)                                            
        at android.app.ActivityThread.main(ActivityThread.java:7229)                    
        at java.lang.reflect.Method.invoke(Native Method)                                
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
 Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Constructor.newInstance(Native Method)
        at android.view.LayoutInflater.createView(LayoutInflater.java:631)         
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:776)                  
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:716)                        
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:847)               
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:810)                         
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:855)                
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:810)                           
        at android.view.LayoutInflater.inflate(LayoutInflater.java:527)                       
        at android.view.LayoutInflater.inflate(LayoutInflater.java:429)                    
        at android.view.LayoutInflater.inflate(LayoutInflater.java:380)                   
        at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:474)               
        at android.app.Activity.setContentView(Activity.java:2387)                       
        at com.android.usemoncore.Activities.TypeActivities.NetActivity.onCreate(NetActivity.java:12)                                                                                        
        at android.app.Activity.performCreate(Activity.java:6876)                                
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)                 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3207)                   
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3350)                    
        at android.app.ActivityThread.access$1100(ActivityThread.java:222)             
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1795)                      
        at android.os.Handler.dispatchMessage(Handler.java:102)                        
        at android.os.Looper.loop(Looper.java:158)                                       
        at android.app.ActivityThread.main(ActivityThread.java:7229)                      
        at java.lang.reflect.Method.invoke(Native Method)                                
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.android.usemoncore.Views.GraphView.setStep(float)' on a null object reference
        at com.android.usemoncore.Views.TypeLinearLayouts.NetLinearLayout.<init>(NetLinearLayout.java:22)                                                             
        at java.lang.reflect.Constructor.newInstance(Native Method)                 
        at android.view.LayoutInflater.createView(LayoutInflater.java:631)       
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:776)                          
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:716)                                   
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:847)                
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:810)                     
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:855)              
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:810)                    
        at android.view.LayoutInflater.inflate(LayoutInflater.java:527)                       
        at android.view.LayoutInflater.inflate(LayoutInflater.java:429)     
        at android.view.LayoutInflater.inflate(LayoutInflater.java:380)                 
        at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:474)                
        at android.app.Activity.setContentView(Activity.java:2387)                      
        at com.android.usemoncore.Activities.TypeActivities.NetActivity.onCreate(NetActivity.java:12)                                                                               
        at android.app.Activity.performCreate(Activity.java:6876)                            
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)            
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3207)               
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3350)             
        at android.app.ActivityThread.access$1100(ActivityThread.java:222)          
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1795)                          
        at android.os.Handler.dispatchMessage(Handler.java:102)                         
        at android.os.Looper.loop(Looper.java:158)                                     
        at android.app.ActivityThread.main(ActivityThread.java:7229)                   
        at java.lang.reflect.Method.invoke(Native Method)                                  
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

这是有问题的 NetLinearLayout.java 代码:

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;

import com.android.usemon.R;
import com.android.usemoncore.Views.GraphView;
import com.android.usemoncore.Views.GraphsLinearLayout;

public class NetLinearLayout extends GraphsLinearLayout {
private GraphView downview;
private GraphView upview;

public NetLinearLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
    downview = (GraphView) findViewById(R.id.downgraph);
    upview = (GraphView) findViewById(R.id.upgraph);
    inflate(context, R.layout.linear_net, this);
    Log.i("NetLinearLayout", "UpView Val:" + upview);
    Log.i("NetLinearLayout", "DownView Val:" + downview);
    this.upview.setStep(this.step);
    this.downview.setStep(this.step);
}

protected void Scale(float multiplier) {
    this.upview.Scale(multiplier);
    this.downview.Scale(multiplier);
}

public void setStep(float step) {
    this.upview.setStep(step);
    this.downview.setStep(step);
}

public void noVLines() {
    this.upview.setNovlines(true);
    this.downview.setNovlines(true);
    this.upview.setTopvertical(false);
}

public void setToShow(int x, int y) {
    this.upview.setToShow(x, y);
    this.downview.setToShow(x, y);
}
}

这是 Linear_net.xml 的代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"
 xmlns:android="http://schemas.android.com/apk/res/android" xmlns:custom="http://schemas.android.com/apk/res-auto">
<com.android.usemoncore.Views.GraphView android:id="@+id/upgraph" android:layout_width="fill_parent" android:layout_height="60.0dip" android:layout_weight="1.0" custom:hlines_to_show="2" custom:vlines_to_show="2" custom:fixed_step="3" custom:vertical_top="true" custom:graphdatatype="netup" />
<com.android.usemoncore.Views.GraphView android:id="@+id/downgraph" android:layout_width="fill_parent" android:layout_height="60.0dip" android:layout_weight="1.0" custom:x_reversed="true" custom:hlines_to_show="2" custom:vlines_to_show="2" custom:fixed_step="3" custom:graphdatatype="netdown" />

这是发生错误的代码行:

this.upview.setStep(this.step);

上面代码中的两个地方都发生了此错误。造成这种情况的原因有哪些?任何帮助将不胜感激。

最佳答案

我认为问题是 XML 和代码之间存在某种不匹配。我期望其中一行或两行:

downview = (GraphView) findViewById(R.id.downgraph);
 upview = (GraphView) findViewById(R.id.upgraph);

返回 null 而不是 View 。您可以添加调试来确认这一点。我认为原因是这些调用时布局没有膨胀。也许您只需要先进行 inflate 调用即可。

我所说的不匹配是指代码只希望获取 ID,但实际上并没有获取到 ID,因此上下文存在问题。看这个SO question从某种意义上说,这个问题过去曾发生过。但我最近遇到的一个问题是试图找到一个位于与 setContentView() 加载的文件不同的文件中的 id。 This SO question帮我找到了。

关于java - Android - 空对象引用错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42498857/

相关文章:

java - Eval() 解释器返回 null

android.permission.BATTERY_STATS 接口(interface)

android - NoClassDefoundError while class is exist in the program

java - 如何从小部件本身手动调用 `onUpdate`

Android:无法将背景可绘制颜色设置为 percentRelativeLayout

android - 在一行中将图像与文本基线对齐

java - 项目构建错误: Non-resolvable parent POM for org. springframework.boot:spring-boot-starter-parent:[未知版本]:传输失败

java - 使用 Spark cassandra 连接器从 cassandra 获取数据时出现问题

java - 在 IntelliJ IDEA 中启用代码样式

android - Textview drawable 不在远程 View 中显示可绘制图像