Android - 空对象引用错误

时间:2017-02-28 02:02:09

标签: 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);

此错误发生在上面代码中的两个位置。造成这种情况的一些原因是什么?任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

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

<View>
    {this.generateColumns(appButtons)}
</View>

返回null而不是视图。您可以添加调试以确认这一点。我认为原因是这些调用时布局没有膨胀。也许你只需先把膨胀电话放好。

由于不匹配我的意思是代码只是希望获取ID,但它没有被获取,因此上下文存在问题。有关此问题在过去发生的一种方式,请参阅此SO question。但是我最近遇到的一个问题是尝试查找与setContentView()加载的文件不同的文件中的id。 This SO question帮助我找到了它。