我有一个问题:我使用的是选项卡式活动,我有2个选项卡。在tab1中,这是一个片段,我试图实现一个搜索栏,但是当我触摸它时,程序崩溃。我该怎么做才能防止这种情况?看来问题出在进度上,因为在onProgessChanged中,我尝试打印一条消息,并且它起作用了。
Frag1.java:
package com.example.myapplication;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.fragment.app.Fragment;
public class Frag1 extends Fragment{
private static final String TAG = "Transfer";
private SeekBar seekBar;
private TextView textSuma;
private TextView textPlata;
@RequiresApi(api = Build.VERSION_CODES.O)
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.tab1_fragment, container, false);
seekBar = (SeekBar) view.findViewById(R.id.seekBar1);
textSuma = (TextView) view.findViewById(R.id.editText);
textPlata = (TextView) view.findViewById(R.id.totalTransferNumber);
seekBar.setMax(1500);
seekBar.setMin(0);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
textPlata.setText(progress);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
return view;
}
}
这是tab1_fragment.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
>
<TextView
android:id="@+id/sumaTransfer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/suma_dorita_pentru_transfer"
android:layout_above="@id/editText"
android:gravity="center"
android:textSize="20sp"
/>
<TextView
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/seekBar1"
android:layout_centerInParent="true"
android:gravity="center"
android:textSize="20sp"
android:padding="5dp" />
<SeekBar
android:id="@+id/seekBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:padding="20dp"/>
<TextView
android:id="@+id/totalTransfer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/total_de_plata"
android:layout_below="@id/seekBar1"
android:gravity="center"
android:textSize="20sp"/>
<TextView
android:id="@+id/totalTransferNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/totalTransfer"
android:gravity="center"
android:padding="16sp"
android:textSize="20sp"
/>
</RelativeLayout>
这是logcat:
2019-12-18 22:54:43.098 1769-1769/com.example.myapplication E/e.myapplicatio: Invalid ID 0x0000003b.
2019-12-18 22:54:43.098 1769-1769/com.example.myapplication E/InputEventReceiver: Exception dispatching input event.
2019-12-18 22:54:43.098 1769-1769/com.example.myapplication D/AndroidRuntime: Shutting down VM
2019-12-18 22:54:43.104 1769-1769/com.example.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myapplication, PID: 1769
android.content.res.Resources$NotFoundException: String resource ID #0x3b
at android.content.res.Resources.getText(Resources.java:367)
at android.widget.TextView.setText(TextView.java:6370)
at com.example.myapplication.Frag1$1.onProgressChanged(Frag1.java:32)
at android.widget.SeekBar.onProgressRefresh(SeekBar.java:98)
at android.widget.ProgressBar.doRefreshProgress(ProgressBar.java:1553)
at android.widget.ProgressBar.refreshProgress(ProgressBar.java:1609)
at android.widget.ProgressBar.setProgressInternal(ProgressBar.java:1675)
at android.widget.AbsSeekBar.trackTouchEvent(AbsSeekBar.java:977)
at android.widget.AbsSeekBar.startDrag(AbsSeekBar.java:933)
at android.widget.AbsSeekBar.onTouchEvent(AbsSeekBar.java:890)
at android.view.View.dispatchTouchEvent(View.java:13415)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3060)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2755)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3060)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2755)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3060)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2755)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3060)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2755)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3060)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2755)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3060)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2755)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3060)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2755)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3060)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2755)
at com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:465)
at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1849)
at android.app.Activity.dispatchTouchEvent(Activity.java:3993)
at androidx.appcompat.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:69)
at com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:423)
at android.view.View.dispatchPointerEvent(View.java:13674)
at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:5482)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:5285)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4788)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4841)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4807)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4947)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4815)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:5004)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4788)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4841)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4807)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4815)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4788)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:7505)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:7474)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:7435)
at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:7630)
at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:188)
at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)
2019-12-18 22:54:43.104 1769-1769/com.example.myapplication E/AndroidRuntime: at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:178)
at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:7581)
at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:7654)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:966)
at android.view.Choreographer.doCallbacks(Choreographer.java:790)
at android.view.Choreographer.doFrame(Choreographer.java:718)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:951)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
2019-12-18 22:54:43.123 1769-1769/com.example.myapplication I/Process: Sending signal. PID: 1769 SIG: 9