有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何解决在安卓的父或祖先上下文中找不到方法:onClick属性?

我试图在用户单击按钮时启动前台服务和通知。此按钮被称为“startBtn”。它旨在显示用户所采取的步骤数

我有3个文件,StepsFragment。java,片段和步骤。xml和StepsService。爪哇

当用户单击按钮时,我在Logcat中遇到此错误:

java.lang.IllegalStateException: Could not find method startService(View) in a parent or ancestor Context for 安卓:onClick attribute defined on view class 安卓.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 安卓:onClick

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

这是我在StepsFragment中的代码。爪哇:

package sg.edu.singaporetech.teamproject;

import 安卓.content.Context;
import 安卓.content.Intent;
import 安卓.hardware.Sensor;
import 安卓.hardware.SensorEvent;
import 安卓.hardware.SensorEventListener;
import 安卓.hardware.SensorManager;
import 安卓.os.Bundle;
import 安卓.support.annotation.Nullable;
import 安卓.support.v4.app.Fragment;
import 安卓.view.LayoutInflater;
import 安卓.view.View;
import 安卓.view.ViewGroup;
import 安卓.widget.Button;
import 安卓.widget.ProgressBar;
import 安卓.widget.TextView;
import 安卓.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);
    }

}

这是片段_步骤中的按钮代码。xml:

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

最后,这是StepsService中的代码。爪哇

package sg.edu.singaporetech.teamproject;

import 安卓.app.Notification;
import 安卓.app.PendingIntent;
import 安卓.app.Service;
import 安卓.content.Intent;
import 安卓.os.IBinder;
import 安卓.support.annotation.Nullable;
import 安卓.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;
    }
}

共 (1) 个答案

  1. # 1 楼答案

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

    另一种方法是在活动中声明startService函数,然后从活动中调用片段方法

    请参阅Android app crashing (fragment and xml onclick)了解更多信息