简体   繁体   中英

Android - Null Object Reference Error

Have some Android code here that is giving me a Null Object Reference Error. Not sure why this is, however I have found out this has to do with the setStep() method. Not sure what is causing this error but here is what I got here in 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:

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:

<?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. 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. 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. See this SO question for one way that this problem has happened in the past. 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(). This SO question helped my find it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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