0

尝试在运行任何版本的 Android 4.x 的模拟器上运行我的 android 应用程序时遇到一些错误,但它在任何版本的 Android 2.X 上都可以正常工作。

package com.EXCLUDED_FOR_PRIVACY.EXCLUDED_FOR_PRIVACY;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;
import android.widget.ImageView;
import android.graphics.Color;

public class EXCLUDED_FOR_PRIVACY extends Activity {

    public int WebView1Bool=0;
    public int WebView2Bool=0;
    public int WebView3Bool=0;
    public int WebView4Bool=0;
    private TabHost tabs;

    private void setupTabHost() {
        tabs = (TabHost) findViewById(R.id.my_tabhost);
        tabs.setup();
    }

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        setupTabHost();
        tabs.getTabWidget().setDividerDrawable(R.drawable.tab_divider);

        setupTab(new ImageView(this), 1);
        setupTab(new ImageView(this), 2);
        setupTab(new ImageView(this), 3);
        setupTab(new ImageView(this), 4);

        WebView mWebView1;
        mWebView1 = (WebView) findViewById(R.id.webview1);
        mWebView1.setBackgroundColor(Color.parseColor("#a3a3a3"));
        mWebView1.setBackgroundColor(0);
        mWebView1.getSettings().setJavaScriptEnabled(true);
        mWebView1.setWebViewClient(new MobileClient());
        mWebView1.loadUrl("EXCLUDED_FOR_PRIVACY");
        WebView1Bool=1;

        tabs.getTabWidget().getChildAt(0).setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v){
                tabs.setCurrentTab(0);
                if(WebView1Bool==0){
                    WebView mWebView1;
                    mWebView1 = (WebView) findViewById(R.id.webview1);
                    mWebView1.setBackgroundColor(Color.parseColor("#a3a3a3"));
                    mWebView1.setBackgroundColor(0);
                    mWebView1.getSettings().setJavaScriptEnabled(true);
                    mWebView1.setWebViewClient(new MobileClient());
                    mWebView1.loadUrl("EXCLUDED_FOR_PRIVACY");
                    WebView1Bool=1;
                }
            }
        });

        tabs.getTabWidget().getChildAt(2).setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v){
                tabs.setCurrentTab(1);
                if(WebView2Bool==0){
                    WebView mWebView2;
                    mWebView2 = (WebView) findViewById(R.id.webview2);
                    mWebView2.setBackgroundColor(Color.parseColor("#a3a3a3"));
                    mWebView2.setBackgroundColor(0);
                    mWebView2.getSettings().setJavaScriptEnabled(true);
                    mWebView2.setWebViewClient(new MobileClient());
                    mWebView2.loadUrl("EXCLUDED_FOR_PRIVACY");
                    WebView2Bool=1;
                }
            }
        });

        tabs.getTabWidget().getChildAt(4).setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v){
                tabs.setCurrentTab(2);
                if(WebView3Bool==0){
                    WebView mWebView3 = (WebView) findViewById(R.id.webview3);
                    mWebView3.setBackgroundColor(Color.parseColor("#a3a3a3"));
                    mWebView3.setBackgroundColor(0);
                    mWebView3.getSettings().setJavaScriptEnabled(true);
                    mWebView3.setWebViewClient(new MobileClient());
                    mWebView3.loadUrl("EXCLUDED_FOR_PRIVACY");
                    WebView3Bool=1;
                }
            }
        });

        tabs.getTabWidget().getChildAt(6).setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v){
                tabs.setCurrentTab(3);
                if(WebView4Bool==0){
                    WebView mWebView4;
                    mWebView4 = (WebView) findViewById(R.id.webview4);
                    mWebView4.setBackgroundColor(Color.parseColor("#a3a3a3"));
                    mWebView4.setBackgroundColor(0);
                    mWebView4.getSettings().setJavaScriptEnabled(true);
                    mWebView4.setWebViewClient(new MobileClient());
                    mWebView4.loadUrl("EXCLUDED_FOR_PRIVACY");
                    WebView4Bool=1;
                }
            }
        });
    }

    private class MobileClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }

    private void setupTab(final View view, final int i) {
        View createTabView_view = LayoutInflater.from(tabs.getContext()).inflate(R.layout.tabs_bg, null);
        ImageView createTabView_tv = (ImageView) createTabView_view.findViewById(R.id.tabsImage);
        if(i==1){
            TabSpec tabSpec = tabs.newTabSpec("EXCLUDED_FOR_PRIVACY");
            createTabView_tv.setImageResource(R.drawable.ic_discounts);
            tabSpec.setIndicator(createTabView_view);
            tabSpec.setContent(R.id.webview1);
            tabs.addTab(tabSpec);
        } else if(i==2){
            TabSpec tabSpec = tabs.newTabSpec("EXCLUDED_FOR_PRIVACY");
            createTabView_tv.setImageResource(R.drawable.ic_specials);
            tabSpec.setIndicator(createTabView_view);
            tabSpec.setContent(R.id.webview2);
            tabs.addTab(tabSpec);
        } else if(i==3){
            TabSpec tabSpec = tabs.newTabSpec("EXCLUDED_FOR_PRIVACY");
            createTabView_tv.setImageResource(R.drawable.ic_directory);
            tabSpec.setIndicator(createTabView_view);
            tabSpec.setContent(R.id.webview3);
            tabs.addTab(tabSpec);
        } else if(i==4){
            TabSpec tabSpec = tabs.newTabSpec("EXCLUDED_FOR_PRIVACY");
            createTabView_tv.setImageResource(R.drawable.ic_membership);
            tabSpec.setIndicator(createTabView_view);
            tabSpec.setContent(R.id.webview4);
            tabs.addTab(tabSpec);
        }
    }
}

这是我看到的错误

09-24 13:11:25.329: E/Trace(1151):              error opening trace file: No such file or directory (2)
09-24 13:11:25.539: D/dalvikvm(1151):           GC_FOR_ALLOC freed 43K, 4% free 8316K/8583K, paused 42ms, total 45ms
09-24 13:11:25.549: I/dalvikvm-heap(1151):      Grow heap (frag case) to 8.829MB for 691216-byte allocation
09-24 13:11:25.669: D/dalvikvm(1151):           GC_CONCURRENT freed <1K, 4% free 8990K/9287K, paused 75ms+6ms, total 124ms
09-24 13:11:25.829: D/gralloc_goldfish(1151):   Emulator without GPU emulation detected.
09-24 13:11:26.209: I/Choreographer(1151):      Skipped 42 frames!  The application may be doing too much work on its main thread.
09-24 13:11:26.379: I/Choreographer(1151):      Skipped 44 frames!  The application may be doing too much work on its main thread.
09-24 13:11:27.509: I/Choreographer(1151):      Skipped 54 frames!  The application may be doing too much work on its main thread.
09-24 13:11:28.319: D/AndroidRuntime(1151):     Shutting down VM
09-24 13:11:28.319: W/dalvikvm(1151):           threadid=1: thread exiting with uncaught exception (group=0x40a13300)
09-24 13:11:28.379: E/AndroidRuntime(1151):     FATAL EXCEPTION: main
09-24 13:11:28.379: E/AndroidRuntime(1151):     java.lang.RuntimeException: Unable to start activity ComponentInfo{com.EXCLUDED.EXCLUDED/com.EXCLUDED.EXCLUDED.EXCLUDED}: java.lang.NullPointerException
09-24 13:11:28.379: E/AndroidRuntime(1151):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
09-24 13:11:28.379: E/AndroidRuntime(1151):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
09-24 13:11:28.379: E/AndroidRuntime(1151):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
09-24 13:11:28.379: E/AndroidRuntime(1151):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
09-24 13:11:28.379: E/AndroidRuntime(1151):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-24 13:11:28.379: E/AndroidRuntime(1151):     at android.os.Looper.loop(Looper.java:137)
09-24 13:11:28.379: E/AndroidRuntime(1151):     at android.app.ActivityThread.main(ActivityThread.java:4745)
09-24 13:11:28.379: E/AndroidRuntime(1151):     at java.lang.reflect.Method.invokeNative(Native Method)
09-24 13:11:28.379: E/AndroidRuntime(1151):     at java.lang.reflect.Method.invoke(Method.java:511)
09-24 13:11:28.379: E/AndroidRuntime(1151):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
09-24 13:11:28.379: E/AndroidRuntime(1151):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-24 13:11:28.379: E/AndroidRuntime(1151):     at dalvik.system.NativeStart.main(Native Method)
09-24 13:11:28.379: E/AndroidRuntime(1151):     Caused by: java.lang.NullPointerException
09-24 13:11:28.379: E/AndroidRuntime(1151):     at com.EXCLUDED.EXCLUDED.EXCLUDED.onCreate(EXCLUDED.java:85)
09-24 13:11:28.379: E/AndroidRuntime(1151):     at android.app.Activity.performCreate(Activity.java:5008)
09-24 13:11:28.379: E/AndroidRuntime(1151):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
09-24 13:11:28.379: E/AndroidRuntime(1151):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
09-24 13:11:28.379: E/AndroidRuntime(1151):     ... 11 more
09-24 13:11:30.289: I/Process(1151):            Sending signal. PID: 1151 SIG: 9

如果这是任何 Java 开发人员都知道的非常简单的事情,我深表歉意,我不得不承认我几乎不知道我在 Java 中做什么,但我正在努力构建它。非常感谢您的帮助。

4

1 回答 1

3

摆脱所有tabs.getTabWidget().getChildAt(...).setOnClickListener()废话,将其替换为TabHost.OnTabChangeListener您在 中注册的单曲TabHost,以了解用户何时更改选项卡。您现有的代码假定 a 的特定内部结构TabWidget,并且正如您所发现的那样,该假设是不可靠的。

于 2012-09-24T18:03:32.920 回答