从TabHost.addTab()抛出ResourceNotFoundException

时间:2012-07-09 00:21:03

标签: resources tabs android-tabhost

我有一个适用于Android操作系统的音板直到ICS。有一个TabHost动态添加了标签,每个标签都有一个由新TabContentFactory创建的不同ScrollView。

在添加选项卡时,它会将此内容吐出到抛出ResourceNotFoundException的日志中。我不会认为我缺少任何资源,因为除了ICS以外它可以正常工作。

我包含了日志和相关代码。还有什么能帮助缩小问题的范围吗?

日志: ...

2012-07-08 16:26:53.507 E 7979/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.lysolpionex.HomestarRunnerSoundboard/com.lysolpionex.HomestarRunnerSoundboard.HomestarRunnerSoundboardActivity}: android.content.res.Resources$NotFoundException: Resource ID #0x0
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2205)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2240)
    at android.app.ActivityThread.access$600(ActivityThread.java:139)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1262)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:156)
    at android.app.ActivityThread.main(ActivityThread.java:5008)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x0
    at android.content.res.Resources.getValue(Resources.java:1105)
    at android.content.res.Resources.loadXmlResourceParser(Resources.java:2344)
    at android.content.res.Resources.getLayout(Resources.java:944)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:394)
    at android.widget.TabHost$LabelAndIconIndicatorStrategy.createIndicatorView(TabHost.java:579)
    at android.widget.TabHost.addTab(TabHost.java:227)
...

创建我的tabhost,向其添加标签等的代码是:

private ViewGroup createTABForm() {
    //construct the TAB host
    TabHost tabHost = new TabHost(this);
    tabHost.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));


    //the tabHost needs a tabWidget, a container for the visible tabs
    TabWidget tabWidget = new TabWidget(this);
    tabWidget.setId(android.R.id.tabs);
    HorizontalScrollView horizScrollView = new HorizontalScrollView(this);
    horizScrollView.addView(tabWidget);
    tabHost.addView(horizScrollView, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

    //the tabHost needs a frameLayout for the views associated with each visible tab
    FrameLayout frameLayout = new FrameLayout(this);
    frameLayout.setId(android.R.id.tabcontent);
    frameLayout.setPadding(0, 100, 0, 0);
    tabHost.addView(frameLayout, new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

    //call setup, since it's not initialized in XML
    tabHost.setup();

    /*-----------------*
     * set up the tabs *
     *-----------------*/
    Iterator<String> namesItr = characters.getNamesItr();
    Iterator<Drawable> drawableItr = characters.getDrawableItr();

    String name = null; //the current character's name
    final Context context = this;
    while(namesItr.hasNext()){  //iterate over all the characters
        name = namesItr.next();   //get this character's name
        final Drawable drawable = drawableItr.next();//the current character's icon
        final String finalName = name;  //silly final variable to satisfy compiler

        //set up the icons
        TabHost.TabSpec tabSpec = tabHost.newTabSpec(name+" Tab");
        tabSpec.setIndicator(name, drawable);

        tabSpec.setContent(new TabHost.TabContentFactory() {

            @Override
            public ScrollView createTabContent(String tag) {
                LinearLayout panel = new LinearLayout(HomestarRunnerSoundboardActivity.this);
                panel.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
                panel.setOrientation(LinearLayout.VERTICAL);

                //iterate over all the sounds for this character
                Iterator<String> titlesItr = characters.getTitlesItrForChar(finalName);
                Iterator<Integer> idsItr = characters.getIDsItrForChar(finalName);
                String title = null;

                while(titlesItr.hasNext() && idsItr.hasNext()){
                    title = titlesItr.next();

                    final Button button = new Button(HomestarRunnerSoundboardActivity.this);
                    button.setText(title);
                    button.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
                    button.setGravity(Gravity.CENTER);
                    final String finalTitle = title;
                    final int finalID = idsItr.next();

                    button.setOnClickListener(new View.OnClickListener(){
                        @Override
                        public void onClick(View v) {
                            SoundManager.playSound(finalTitle);
                        }
                    });

                    /*---------------------------------------------------------------------------
                     * Sets the long-click action, in this case to save a sound and set it as a *
                     * ringtone/notification                                                    *
                     *--------------------------------------------------------------------------*/
                    button.setOnLongClickListener(new View.OnLongClickListener() {
                        @Override
                        public boolean onLongClick(View v) {
                        //save the file to the sd card
                        if(setFileAsRingAndNotifSound(finalID, finalTitle)){
                            Toast.makeText(HomestarRunnerSoundboardActivity.this, "Sound added to ringtones/alerts", Toast.LENGTH_SHORT).show();
                        }
                        else{
                            Toast.makeText(HomestarRunnerSoundboardActivity.this, "Error adding sound to ringtones/alerts", Toast.LENGTH_LONG).show();
                            Log.e(Utils.hsrTAG, "Error setting file as ringtone/notification sound.");
                        }
                        return true;
                      }
                    });
                    button.setMinWidth(m_nScreenW); //do I need this?
                    panel.addView(button);
                }

                ScrollView scrollView = new ScrollView(context);
                scrollView.addView(panel);

                return scrollView;
            }
        });

        tabHost.addTab(tabSpec);
    }

    return tabHost;
}

1 个答案:

答案 0 :(得分:0)

我有同样的问题,但有点不同,在stackoverflow内搜索我发现: 我认为它可以帮到你

Honeycomb and TabHost specs