简体   繁体   English

Android-空对象引用错误

[英]Android - Null Object Reference Error

Have some Android code here that is giving me a Null Object Reference Error. 这里有一些Android代码,给了我一个Null Object Reference Error。 Not sure why this is, however I have found out this has to do with the setStep() method. 不知道为什么会这样,但是我发现这与setStep()方法有关。 Not sure what is causing this error but here is what I got here in logcat: 不知道是什么导致了此错误,但这是我在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)

Here is the code for NetLinearLayout.java that it has an issue with: 这是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);
}
}

Here is the code of linear_net.xml: 这是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" />

Here is the line of code where the error is taking place: 这是发生错误的代码行:

this.upview.setStep(this.step);

This error is taking place both places in the code above. 上面的代码中的两个地方都发生此错误。 What are some of the things that are causing this? 是什么原因引起的呢? Any help would be greatly appreciated. 任何帮助将不胜感激。

I think the problem is some kind of mismatch between the XML and the code. 我认为问题是XML与代码之间的某种不匹配。 I expect one or both of the lines: 我希望这两行中的一个或两个都行:

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

Is returning a null instead of a view. 返回null而不是视图。 You can add debugging to confirm this. 您可以添加调试以确认这一点。 I think that the cause is that the layout is not inflated at the time of these calls. 我认为原因是这些调用时布局没有膨胀。 Maybe you just need to put the inflate call first. 也许您只需要将充气电话放在首位即可。

By mismatch I mean that the code just expects the ID to get fetched, but it isn't getting fetched so there is a problem with the context. 通过不匹配,我的意思是代码仅希望获取ID,但不会获取ID,因此上下文存在问题。 See this SO question for one way that this problem has happened in the past. 请参阅此SO问题 ,以了解该问题过去发生过的一种方式。 But one that I have run into recently was trying to find an id that was in a different file than the file that was loaded by setContentView(). 但是我最近遇到的一个问题是试图找到一个与setContentView()加载的文件不在同一个文件中的ID。 This SO question helped my find it. 这样的问题帮助我找到了它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM