我看了2015的資料,他所有程式都寫在OnCreate裡,但是我用新版裡,但是我用新版android studio雖然沒有錯誤訊息,但是會直接閃退,我查了文章,他說要放到OnCreateView哩,但是不會用OnCreateView阿,找不到中文資料又看不懂英文
附上閃退的程式
java
package com.example.cntwleandroidalarm;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TabHost;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabHost = (TabHost) findViewById(android.R.id.tabhost);
tabHost.setup();
tabHost.addTab(tabHost.newTabSpec("tabTime").setIndicator("時鐘").setContent(R.id.tabTime));
tabHost.addTab(tabHost.newTabSpec("tabAlare").setIndicator("鬧鐘").setContent(R.id.tabAlarm));
tabHost.addTab(tabHost.newTabSpec("tabTimer").setIndicator("計時器").setContent(R.id.tabTimer));
tabHost.addTab(tabHost.newTabSpec("tabStopWatch").setIndicator("秒表").setContent(R.id.tabStopWatch));
}
private TabHost tabHost;
}
HTML
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TabHost
android:layout_width="369dp"
android:layout_height="630dp"
android:translationX="0dp"
android:translationY="-105dp"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="106dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="556dp"
android:orientation="vertical">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="504dp">
<LinearLayout
android:id="@+id/tabTime"
android:layout_width="369dp"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
<LinearLayout
android:id="@+id/tabAlarm"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
<LinearLayout
android:id="@+id/tabTimer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
<LinearLayout
android:id="@+id/tabStopWatch"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
tools:layout_editor_absoluteX="369dp"
tools:layout_editor_absoluteY="89dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
加這段android:id="@android:id/tabhost"
變成
<TabHost
android:id="@android:id/tabhost"
android:layout_width="369dp"
android:layout_height="630dp"
android:translationX="0dp"
android:translationY="-105dp"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="106dp">
即可