2017-02-28 120 views
0

在這裏有一些Android代碼給我一個空對象引用錯誤。不知道這是爲什麼,但我發現這與setStep()方法有關。不知道是什麼原因造成這個錯誤,但這裏是我在這裏的logcat:Android - 空對象引用錯誤

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" /> 

這裏是代碼行,其中的錯誤是德ing地方:

this.upview.setStep(this.step); 

這個錯誤發生在上面兩個地方。導致這一點的一些事情是什麼?任何幫助將不勝感激。

+1

這是什麼? this.step –

回答

1

我認爲問題是XML和代碼之間的某種不匹配。我期待一行或兩行:

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

正在返回null而不是視圖。您可以添加調試來確認這一點。我認爲這是因爲這些調用時佈局沒有膨脹。也許你只需要首先打出充氣電話。

由不匹配我的意思是說,代碼只是預計ID獲取,但它沒有被提取,所以上下文有問題。查看這SO question爲過去發生此問題的一種方法。但是最近我遇到的一個問題是試圖找到與setContentView()加載的文件不同的文件。 This SO question幫助我找到它。