我有一个在 ICS 之前可以在 Android 操作系统中使用的音板。有一个 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)
...
创建我的标签主机、向其添加标签等的代码是:
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;
}