我尝试制作一个应用程序,该应用程序以具有按钮的活动开始,该按钮将打开包含3个不同选项卡的tabhost。我正在尝试使用其中一个选项卡上的按钮更改TextView上的文本。问题是点击第一个按钮(打开tabhost的按钮)后,应用程序停止工作。
activity_main.xml中()
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.softeng.appblocker.MainActivity">
<Button
android:id="@+id/btnLaunch"
android:layout_width="354dp"
android:layout_height="345dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="@string/block_me"
android:textSize="50sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.050000012" />
<TextView
android:id="@+id/textView7"
android:layout_width="166dp"
android:layout_height="45dp"
android:layout_marginBottom="8dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:text="@string/total_time_used"
android:textAppearance="@style/TextAppearance.AppCompat.Caption"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnLaunch"
app:layout_constraintVertical_bias="0.152" />
<TextView
android:id="@+id/textView8"
android:layout_width="176dp"
android:layout_height="45dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="16dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:textAlignment="textEnd"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toEndOf="@+id/textView7"
app:layout_constraintTop_toBottomOf="@+id/btnLaunch"
app:layout_constraintVertical_bias="0.151" />
<TextView
android:id="@+id/textView9"
android:layout_width="166dp"
android:layout_height="46dp"
android:layout_marginBottom="8dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:text="@string/total_wins"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView7"
app:layout_constraintVertical_bias="0.465" />
<TextView
android:id="@+id/textView10"
android:layout_width="176dp"
android:layout_height="45dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:textAlignment="textEnd"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.2"
app:layout_constraintStart_toEndOf="@+id/textView9"
app:layout_constraintTop_toBottomOf="@+id/textView8"
app:layout_constraintVertical_bias="0.458" />
</android.support.constraint.ConstraintLayout>
MaiActivity.java
package com.softeng.appblocker;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
public Button btnLaunch;
public void init(){
btnLaunch = (Button)findViewById(R.id.btnLaunch);
btnLaunch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v){
Intent toy = new Intent(MainActivity.this, Body.class);
startActivity(toy);
}
});
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
}
activity_body.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.softeng.appblocker.Body">
<TabHost
android:id="@+id/tabHost"
android:layout_width="385dp"
android:layout_height="581dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="top|bottom"
android:orientation="vertical">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="top" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="@+id/blocked"
layout="@layout/blocked"
android:layout_width="match_parent"
android:layout_height="match_parent"></include>
<include
android:id="@+id/timer"
layout="@layout/time"
android:layout_width="match_parent"
android:layout_height="match_parent"></include>
<include
android:id="@+id/apps"
layout="@layout/apps"
android:layout_width="369dp"
android:layout_height="434dp"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp"></include>
</FrameLayout>
</LinearLayout>
</TabHost>
</android.support.constraint.ConstraintLayout>
Body.java
package com.softeng.appblocker;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TabHost;
import android.widget.TextView;
public class Body extends AppCompatActivity {
public static TextView timer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_body);
TabHost tab = (TabHost) findViewById(R.id.tabHost);
tab.setup();
TabHost.TabSpec spec1 = tab.newTabSpec("Apps");
spec1.setIndicator("Apps");
spec1.setContent(R.id.apps);
tab.addTab(spec1);
TabHost.TabSpec spec2 = tab.newTabSpec("Blocked");
spec2.setIndicator("Blocked");
spec2.setContent(R.id.blocked);
tab.addTab(spec2);
TabHost.TabSpec spec3 = tab.newTabSpec("Time");
spec3.setIndicator("Time");
spec3.setContent(R.id.time);
tab.addTab(spec3);
Button btnstart = (Button) findViewById(R.id.start);
timer = (TextView) findViewById(R.id.timer);
btnstart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
timer.setText("ding ding ding");
}
});
}
}
logcat的
12-22 08:55:46.025 17711-17711/? I/art: Late-enabling -Xcheck:jni
12-22 08:55:46.381 17711-17711/? W/System: ClassLoader referenced unknown path: /data/app/com.softeng.appblocker-2/lib/arm
12-22 08:55:46.401 17711-17711/? I/InstantRun: starting instant run server: is main process
12-22 08:55:46.546 17711-17711/? W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
12-22 08:55:46.846 17711-17711/? V/PhoneWindow: DecorView setVisiblity: visibility = 4, Parent = null, this = DecorView@c543a71[]
12-22 08:55:46.854 17711-17711/? D/WindowClient: Add to mViews: DecorView@c543a71[MainActivity], this = android.view.WindowManagerGlobal@f866054
12-22 08:55:46.855 17711-17711/? D/OpenGLRenderer: Dumper init 2 threads <0xb15937c0>
12-22 08:55:46.855 17711-17711/? D/OpenGLRenderer: <com.softeng.appblocker> is running.
12-22 08:55:46.858 17711-17727/? D/OpenGLRenderer: CanvasContext() 0x9a1d6000
12-22 08:55:46.877 17711-17711/? D/GraphicBuffer: register, handle(0xb1634ac0) (w:960 h:576 s:960 f:0x1 u:0x000100)
12-22 08:55:46.878 17711-17711/? D/ViewRootImpl[MainActivity]: hardware acceleration is enabled, this = ViewRoot{288cbfd com.softeng.appblocker/com.softeng.appblocker.MainActivity,ident = 0}
12-22 08:55:46.882 17711-17711/? V/PhoneWindow: DecorView setVisiblity: visibility = 0, Parent = ViewRoot{288cbfd com.softeng.appblocker/com.softeng.appblocker.MainActivity,ident = 0}, this = DecorView@c543a71[MainActivity]
12-22 08:55:46.929 17711-17711/? D/Surface: Surface::allocateBuffers(this=0xa995c700)
12-22 08:55:46.929 17711-17727/? D/OpenGLRenderer: CanvasContext() 0x9a1d6000 initialize window=0xa995c700, title=com.softeng.appblocker/com.softeng.appblocker.MainActivity
12-22 08:55:46.939 17711-17727/? I/OpenGLRenderer: Initialized EGL, version 1.4
12-22 08:55:46.939 17711-17727/? D/OpenGLRenderer: Swap behavior 1
12-22 08:55:46.944 17711-17727/? D/OpenGLRenderer: Created EGL context (0xa996d900)
12-22 08:55:46.950 17711-17727/? D/OpenGLRenderer: ProgramCache.init: enable enhancement 1
12-22 08:55:46.950 17711-17727/? I/OpenGLRenderer: Get disable program binary service property (0)
12-22 08:55:46.950 17711-17727/? I/OpenGLRenderer: Initializing program atlas...
12-22 08:55:46.952 17711-17727/? I/ProgramBinary/Service: ProgramBinaryService client side disable debugging.
12-22 08:55:46.952 17711-17727/? I/ProgramBinary/Service: ProgramBinaryService client side disable binary content debugging.
12-22 08:55:46.952 17711-17727/? D/ProgramBinary/Service: BpProgramBinaryService.getReady
12-22 08:55:46.953 17711-17727/? D/ProgramBinary/Service: BpProgramBinaryService.getProgramBinaryData
12-22 08:55:46.953 17711-17727/? I/OpenGLRenderer: Program binary detail: Binary length is 182828, program map length is 124.
12-22 08:55:46.953 17711-17727/? I/OpenGLRenderer: Succeeded to mmap program binaries. File descriptor is 69, and path is /dev/ashmem.
12-22 08:55:46.954 17711-17727/? I/OpenGLRenderer: No need to use file discriptor anymore, close fd(69).
12-22 08:55:46.954 17711-17727/? D/OpenGLRenderer: Initializing program cache from 0x0, size = -1
12-22 08:55:46.959 17711-17727/? D/Surface: Surface::connect(this=0xa995c700,api=1)
12-22 08:55:46.960 17711-17727/? W/libEGL: [ANDROID_RECORDABLE] format: 1
12-22 08:55:46.962 17711-17727/? D/mali_winsys: EGLint new_window_surface(egl_winsys_display*, void*, EGLSurface, EGLConfig, egl_winsys_surface**, egl_color_buffer_format*, EGLBoolean) returns 0x3000
12-22 08:55:46.992 17711-17711/? W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
12-22 08:55:47.054 17711-17727/? D/OpenGLRenderer: CacheTexture 3 upload: x, y, width height = 0, 0, 465, 195
12-22 08:55:47.054 17711-17727/? D/OpenGLRenderer: ProgramCache.generateProgram: 0
12-22 08:55:47.062 17711-17727/? D/GraphicBuffer: register, handle(0xb1634dc0) (w:720 h:1280 s:720 f:0x1 u:0x000b00)
12-22 08:55:47.070 17711-17727/? D/OpenGLRenderer: ProgramCache.generateProgram: 34359738371
12-22 08:55:47.073 17711-17727/? D/OpenGLRenderer: ProgramCache.generateProgram: 240518168576
12-22 08:55:47.075 17711-17727/? D/OpenGLRenderer: ProgramCache.generateProgram: 68724719680
12-22 08:55:47.087 17711-17727/? D/GraphicBuffer: register, handle(0xb1634d00) (w:720 h:1280 s:720 f:0x1 u:0x000b00)
12-22 08:55:47.111 17711-17727/? D/OpenGLRenderer: ProgramCache.generateProgram: 103084458052
12-22 08:55:47.121 17711-17727/? D/GraphicBuffer: register, handle(0xb1634e80) (w:720 h:1280 s:720 f:0x1 u:0x000b00)
12-22 08:56:00.548 17711-17711/com.softeng.appblocker V/PhoneWindow: DecorView setVisiblity: visibility = 0, Parent = ViewRoot{288cbfd com.softeng.appblocker/com.softeng.appblocker.MainActivity,ident = 0}, this = DecorView@c543a71[MainActivity]
12-22 08:56:01.162 17711-17711/com.softeng.appblocker V/InputMethodManager: onWindowFocus: null softInputMode=288 first=true flags=#81810100
12-22 08:56:02.221 17711-17711/com.softeng.appblocker D/AndroidRuntime: Shutting down VM
12-22 08:56:02.229 17711-17711/com.softeng.appblocker E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.softeng.appblocker, PID: 17711
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.softeng.appblocker/com.softeng.appblocker.Body}: java.lang.RuntimeException: Could not create tab content because could not find view with id 2131230893
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2724)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2789)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1527)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:203)
at android.app.ActivityThread.main(ActivityThread.java:6251)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)
Caused by: java.lang.RuntimeException: Could not create tab content because could not find view with id 2131230893
at android.widget.TabHost$ViewIdContentStrategy.<init>(TabHost.java:663)
at android.widget.TabHost$ViewIdContentStrategy.<init>(TabHost.java)
at android.widget.TabHost$TabSpec.setContent(TabHost.java:503)
at com.softeng.appblocker.Body.onCreate(Body.java:32)
at android.app.Activity.performCreate(Activity.java:6666)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2677)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2789)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1527)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:203)
at android.app.ActivityThread.main(ActivityThread.java:6251)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)
答案 0 :(得分:0)
对你有一些建议。
1.使用相同的ID
根据您的xml和活动代码。
你的xml代码
<include
android:id="@+id/timer"
layout="@layout/time"
android:layout_width="match_parent"
android:layout_height="match_parent"></include>
Body.java
TabHost.TabSpec spec3 = tab.newTabSpec("Time");
spec3.setIndicator("Time");
spec3.setContent(R.id.time);
tab.addTab(spec3);
xml中的ID为timer
,Body.java
中的ID为time
。 id
不同。所以你应该让它们变得一样。
您可以更改所有time
。因为TextView
的帖子是timer
。只需使用其他ID。
2.检查您的xml代码中是否有btnstart
和timer
。