java - 我如何解决在 android :onClick attribute? 的父或祖先上下文中找不到方法

标签 java android fragment

我正在尝试在用户单击按钮时启动前台服务和通知。这个按钮叫做“startBtn”。它旨在显示用户采取的步骤数。

我有 3 个文件,StepsFragment.java、fragment_steps.xml 和 StepsService.java。

当用户点击按钮时,我在 Logcat 中遇到了这个错误:

java.lang.IllegalStateException: Could not find method startService(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'startBtn'

我已经尝试了以下在 StackOverflow 上提出的解决方案:

How to solve error: Could not find method onClick(View) in a parent or ancestor Context for android:onClick

Could not find method in a parent or ancestor Context for android:onClick attribute

这是我在 StepsFragment.java 中的代码:

package sg.edu.singaporetech.teamproject;

import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class StepsFragment extends Fragment implements SensorEventListener, View.OnClickListener {

    Button updateBtn;
    ProgressBar progressBar;
    TextView tv_steps;
    TextView levelDisplay;
    SensorManager sensorManager;
    Sensor sensor;
    boolean running = false;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {


        View view = inflater.inflate(R.layout.fragment_steps, container, false);

        super.onCreate ( savedInstanceState );
        updateBtn = view.findViewById(R.id.updateBtn);
        progressBar = view.findViewById(R.id.progress_bar);
        tv_steps = (TextView) view.findViewById(R.id.tv_steps);
        levelDisplay = (TextView) view.findViewById(R.id.levelDisplay);
        sensorManager = (SensorManager) getActivity().getSystemService ( Context.SENSOR_SERVICE);

        updateBtn.setOnClickListener(this);

        return view;

    }

    public void startService(View view) {
        String input = tv_steps.getText().toString();
        Intent serviceIntent = new Intent(getActivity(),StepsService.class);
        serviceIntent.putExtra("inputExtra",input);

        getActivity().startService(serviceIntent);
    }

}

这是 fragment_steps.xml 中的按钮代码:

    <android.support.v7.widget.AppCompatButton
        android:id="@+id/startBtn"
        style="?android:attr/borderlessButtonStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:background="@drawable/btn_orange"
        android:text="@string/start_steps"
        android:textColor="@color/white"
        android:textSize="15dp"
        android:onClick="startService"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/updateBtn" />

最后,这是 StepsService.java 中的代码

package sg.edu.singaporetech.teamproject;

import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.v4.app.NotificationCompat;

import static sg.edu.singaporetech.teamproject.StepsNotification.CHANNEL_ID;

public class StepsService extends Service {

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        String input = intent.getStringExtra("inputExtra");

        Intent notificationIntent = new Intent(this,StepsFragment.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this,0,notificationIntent,0);

        Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setContentTitle("Steps Tracker")
                .setContentText("Steps tracked")
                .setSmallIcon(R.drawable.exersize)
                .setContentIntent(pendingIntent)
                .build();
        startForeground(1,notification);

        return START_NOT_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}

最佳答案

android:onClick="startService" 不会在 fragment 中调用。您应该使用 OnClickListener 来处理此类事件。

另一种方法是在 Activity 中声明startService 函数,然后从 Activity 中调用 fragment 方法。

请引用Android app crashing (fragment and xml onclick)了解更多。

关于java - 我如何解决在 android :onClick attribute? 的父或祖先上下文中找不到方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55409957/

相关文章:

java - 字符串正斜杠被替换

java - 使用服务器套接字后如何关闭端口

android - 适用于 Android 的 ISO 适用于 VirtualBox 的 Google API

android - android Fragment 的 PagerAdapter 问题

android - 返回 Activity 时为空指针

android - 转到上一个 fragment 时如何避免关闭 Activity

java - (为什么)Tomcat/Java 在 Linux 上比在 Windows 上表现更好?

java - Spring oAuth2 中的 AuthenticationScheme 和 ClientAuthenticationScheme 有什么区别?

android - 单击时更新按钮的不透明度? flutter

android - Android 中的 Unicode 问题