Android App - 在Eclair上返回空指针异常

时间:2010-12-14 18:04:25

标签: java android

我正在编写一个我正在编写的Android应用程序的问题。由于NullPointerException,它在Eclair上崩溃,但在Froyo和Gingerbread上运行正常。任何人都可以告诉我如何重写这个以避免空指针异常?提前谢谢。

修改 我添加了堆栈跟踪和logcat,我原本可能读错了,看起来可能是我的TabWidget

logcat在我的Home类中引用了这一行:

tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("Snow", res.getDrawable(R.drawable.weather2)).setContent(new Intent(this,FirstTab.class))); 

logcat的

ERROR/AndroidRuntime(314): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.wilcoxis.android.kirkwood/com.wilcoxis.android.Home}: java.lang.NullPointerException
ERROR/AndroidRuntime(314):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
ERROR/AndroidRuntime(314):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
ERROR/AndroidRuntime(314):     at android.app.ActivityThread.access$2200(ActivityThread.java:119)
ERROR/AndroidRuntime(314):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
ERROR/AndroidRuntime(314):     at android.os.Handler.dispatchMessage(Handler.java:99)
ERROR/AndroidRuntime(314):     at android.os.Looper.loop(Looper.java:123)
ERROR/AndroidRuntime(314):     at android.app.ActivityThread.main(ActivityThread.java:4363)
ERROR/AndroidRuntime(314):     at java.lang.reflect.Method.invokeNative(Native Method)
ERROR/AndroidRuntime(314):     at java.lang.reflect.Method.invoke(Method.java:521)
ERROR/AndroidRuntime(314):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
ERROR/AndroidRuntime(314):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
ERROR/AndroidRuntime(314):     at dalvik.system.NativeStart.main(Native Method)
ERROR/AndroidRuntime(314): Caused by: java.lang.NullPointerException
ERROR/AndroidRuntime(314):     at android.widget.TabWidget.onFocusChange(TabWidget.java:351)
ERROR/AndroidRuntime(314):     at android.view.View.onFocusChanged(View.java:2622)
ERROR/AndroidRuntime(314):     at android.view.View.handleFocusGainInternal(View.java:2445)
ERROR/AndroidRuntime(314):     at android.view.ViewGroup.handleFocusGainInternal(ViewGroup.java:404)
ERROR/AndroidRuntime(314):     at android.view.View.requestFocus(View.java:3562)
ERROR/AndroidRuntime(314):     at android.view.ViewGroup.requestFocus(ViewGroup.java:976)
ERROR/AndroidRuntime(314):     at android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:1021)
ERROR/AndroidRuntime(314):     at android.view.ViewGroup.requestFocus(ViewGroup.java:977)
ERROR/AndroidRuntime(314):     at android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:1021)
ERROR/AndroidRuntime(314):     at android.view.ViewGroup.requestFocus(ViewGroup.java:980)
ERROR/AndroidRuntime(314):     at android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:1021)
ERROR/AndroidRuntime(314):     at android.view.ViewGroup.requestFocus(ViewGroup.java:977)
ERROR/AndroidRuntime(314):     at android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:1021)
ERROR/AndroidRuntime(314):     at android.view.ViewGroup.requestFocus(ViewGroup.java:980)
ERROR/AndroidRuntime(314):     at android.view.View.requestFocus(View.java:3513)
ERROR/AndroidRuntime(314):     at android.view.View.requestFocus(View.java:3491)
ERROR/AndroidRuntime(314):     at android.widget.TabHost.setCurrentTab(TabHost.java:334)
ERROR/AndroidRuntime(314):     at android.widget.TabHost.addTab(TabHost.java:213)
ERROR/AndroidRuntime(314):     at com.wilcoxis.android.Home.onCreate(Home.java:83)
ERROR/AndroidRuntime(314):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
ERROR/AndroidRuntime(314):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
ERROR/AndroidRuntime(314):     ... 11 more

堆栈跟踪

DalvikVM[localhost:8610]    
    Thread [<3> main] (Suspended (exception NullPointerException))  
        TabWidget.onFocusChange(View, boolean) line: 351    
        TabWidget(View).onFocusChanged(boolean, int, Rect) line: 2622   
        TabWidget(View).handleFocusGainInternal(int, Rect) line: 2445   
        TabWidget(ViewGroup).handleFocusGainInternal(int, Rect) line: 404   
        TabWidget(View).requestFocus(int, Rect) line: 3562  
        TabWidget(ViewGroup).requestFocus(int, Rect) line: 976  
        LinearLayout(ViewGroup).onRequestFocusInDescendants(int, Rect) line: 1021   
        LinearLayout(ViewGroup).requestFocus(int, Rect) line: 977   
        TabHost(ViewGroup).onRequestFocusInDescendants(int, Rect) line: 1021    
        TabHost(ViewGroup).requestFocus(int, Rect) line: 980    
        FrameLayout(ViewGroup).onRequestFocusInDescendants(int, Rect) line: 1021    
        FrameLayout(ViewGroup).requestFocus(int, Rect) line: 977    
        PhoneWindow$DecorView(ViewGroup).onRequestFocusInDescendants(int, Rect) line: 1021  
        PhoneWindow$DecorView(ViewGroup).requestFocus(int, Rect) line: 980  
        PhoneWindow$DecorView(View).requestFocus(int) line: 3513    
        PhoneWindow$DecorView(View).requestFocus() line: 3491   
        TabHost.setCurrentTab(int) line: 334    
        TabHost.addTab(TabHost$TabSpec) line: 213   
        Home.onCreate(Bundle) line: 83  
        Instrumentation.callActivityOnCreate(Activity, Bundle) line: 1047   
        ActivityThread.performLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2459  
        ActivityThread.handleLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2512   
        ActivityThread.access$2200(ActivityThread, ActivityThread$ActivityRecord, Intent) line: 119 
        ActivityThread$H.handleMessage(Message) line: 1863  
        ActivityThread$H(Handler).dispatchMessage(Message) line: 99 
        Looper.loop() line: 123 
        ActivityThread.main(String[]) line: 4363    
        Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]  
        Method.invoke(Object, Object...) line: 521  
        ZygoteInit$MethodAndArgsCaller.run() line: 860  
        ZygoteInit.main(String[]) line: 618 
        NativeStart.main(String[]) line: not available [native method]  
    Thread [<13> Binder Thread #2] (Running)    
    Thread [<11> Binder Thread #1] (Running)

1 个答案:

答案 0 :(得分:0)

您没有处理从服务返回空字符串的情况。实际上,你没有处理任何失败的情况。检查您是否获得了有效数据,并且不对可能为null的引用变量(如字符串数组)进行操作