我有下一个代码:
public class YazCalcActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost tabHost = (TabHost)this.findViewById(R.id.tabhost);
tabHost.setup();
TabHost.TabSpec calculateSpec = tabHost.newTabSpec("Calculate");
calculateSpec.setIndicator("Calculate");
calculateSpec.setContent(R.id.calculateLayout);
TabHost.TabSpec solveSpec = tabHost.newTabSpec("Solve");
solveSpec.setIndicator("Solve");
solveSpec.setContent(R.id.solveLayout);
tabHost.addTab(calculateSpec);
tabHost.addTab(solveSpec);
tabHost.setCurrentTab(0);
}
}
main.xml是:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TabHost
android:id="@+id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:id="@+id/calculateLayout" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:id="@+id/solveLayout" >
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckBox" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
问题是,如果我将calculateLayout和solveLayout的代码粘贴到单独的XML文件中并执行calculateSpec.setContent(R.layouts.calculatelayout)而不是calculateSpec.setContent(R.id.calculateLayout) - 应用程序在启动时崩溃。
为什么???
UPD 错误日志:
02-10 19:55:02.988:W / dalvikvm(31196):threadid = 3:线程退出 未捕获的异常(组= 0x4001b168)02-10 19:55:02.988: E / AndroidRuntime(31196):未捕获的处理程序:由于线程主要退出 未捕获的异常02-10 19:55:03.018:E / AndroidRuntime(31196): java.lang.RuntimeException:无法启动活动 ComponentInfo {com.ia01.yazcalc / com.ia01.yazcalc.YazCalcActivity}: java.lang.RuntimeException:无法创建选项卡内容,因为可以 找不到id为2131034112 02-10 19:55:03.018的视图: E / AndroidRuntime(31196):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496) 02-10 19:55:03.018:E / AndroidRuntime(31196):at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512) 02-10 19:55:03.018:E / AndroidRuntime(31196):at android.app.ActivityThread.access $ 2200(ActivityThread.java:119)02-10 19:55:03.018:E / AndroidRuntime(31196):at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1863) 02-10 19:55:03.018:E / AndroidRuntime(31196):at android.os.Handler.dispatchMessage(Handler.java:99)02-10 19:55:03.018:E / AndroidRuntime(31196):at android.os.Looper.loop(Looper.java:123)02-10 19:55:03.018: E / AndroidRuntime(31196):at android.app.ActivityThread.main(ActivityThread.java:4363)02-10 19:55:03.018:E / AndroidRuntime(31196):at java.lang.reflect.Method.invokeNative(Native Method)02-10 19:55:03.018:E / AndroidRuntime(31196):at java.lang.reflect.Method.invoke(Method.java:521)02-10 19:55:03.018: E / AndroidRuntime(31196):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:860) 02-10 19:55:03.018:E / AndroidRuntime(31196):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)02-10 19:55:03.018:E / AndroidRuntime(31196):at dalvik.system.NativeStart.main(Native Method)02-10 19:55:03.018: E / AndroidRuntime(31196):引起:java.lang.RuntimeException:可以 不创建选项卡内容,因为找不到ID为2131034112的视图 02-10 19:55:03.018:E / AndroidRuntime(31196):at android.widget.TabHost $ ViewIdContentStrategy。(TabHost.java:587) 02-10 19:55:03.018:E / AndroidRuntime(31196):at android.widget.TabHost $ ViewIdContentStrategy。(TabHost.java:578) 02-10 19:55:03.018:E / AndroidRuntime(31196):at android.widget.TabHost $ TabSpec.setContent(TabHost.java:435)02-10 19:55:03.018:E / AndroidRuntime(31196):at com.ia01.yazcalc.YazCalcActivity.onCreate(YazCalcActivity.java:18) 02-10 19:55:03.018:E / AndroidRuntime(31196):at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 02-10 19:55:03.018:E / AndroidRuntime(31196):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459) 02-10 19:55:03.018:E / AndroidRuntime(31196):... 11 more
UPD2
我已经清理了这个项目,但没有任何改变。但。我找到了有关此问题的信息。我不能直接将“布局”设置为内容,因为我需要首先创建View这个布局。所以我以下一种方式更改了代码:
public class YazCalcActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost tabHost = (TabHost)this.findViewById(R.id.tabhost);
tabHost.setup();
TabHost.TabSpec calculateSpec = tabHost.newTabSpec("Calculate");
calculateSpec.setIndicator("Calculate");
View calculateView = this.getLayoutInflater().inflate(R.layout.calculatelayout, null);
calculateSpec.setContent(calculateView.getId());
TabHost.TabSpec solveSpec = tabHost.newTabSpec("Solve");
solveSpec.setIndicator("Solve");
View solveView = this.getLayoutInflater().inflate(R.layout.solvelayout, null);
calculateSpec.setContent(solveView.getId());
tabHost.addTab(calculateSpec);
tabHost.addTab(solveSpec);
tabHost.setCurrentTab(0);
}
}
但是,我仍然得到同样的错误。哇...
upd3 已解决。
好吧,我已经修好了这个狗屎。我只需要使用“包含”:
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<include layout="@layout/calculate_layout"
android:id="@+id/calculateTab" />
<include layout="@layout/solve_layout"
android:id="@+id/solveTab" />
</FrameLayout>
答案 0 :(得分:0)
基于日志
Could not create tab content because could not find view with id 2131034112
执行以下操作:
1-保存所有文件。 2-选择项目&gt;清洁
尝试重新运行该程序,并告诉我它是否仍无效。
答案 1 :(得分:0)
好吧,我已经解决了这个问题。我只需要使用“包含”:
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<include layout="@layout/calculate_layout"
android:id="@+id/calculateTab" />
<include layout="@layout/solve_layout"
android:id="@+id/solveTab" />
</FrameLayout>