我该如何解决在android:onClick属性的父级或祖先上下文中找不到方法?

吴志刚:

我正在尝试在用户单击按钮时启动前台服务和通知。此按钮称为“ 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的解决方案:

如何解决错误:在android:onClick的父级或祖先上下文中找不到方法onClick(View)

在android:onClick属性的父级或祖先上下文中找不到方法

这是我在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" 不会分段调用您应该改用OnClickListener处理此类事件。

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

请参考Android应用程序崩溃(片段和xml onclick)以了解更多信息。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何解决错误:在android:onClick的父级或祖先上下文中找不到方法onClick(View)

如何调试“在Android的父级或祖先上下文中找不到方法showTimePickerDialog(View)”?

在父级或祖先上下文中找不到方法

无法在父级或祖先上下文中找到方法 StartRec(View)

IllegalStateException:在父级或祖先上下文类android.support.v7.widget.AppCompatButton中找不到方法insert(View)

我该如何解决:名称“productQuantity”在当前上下文中不存在

如何在移动优先上下文中为reps.imgs设置'sizes'属性

如何解决此错误?:无法从静态上下文中引用非静态方法 maxOf(int,int)

PHP-如何解决错误“不在对象上下文中时使用$ this”?

如何解决此错误,无法在上下文中找到实用程序

如何解决tableView上下文中的“ Reactive <_>'模棱两可”错误

如何解决“名称'WebSecurity'在当前上下文中不存在”?

如何解决“词汇声明不能出现在单语句上下文中”

当前上下文中不存在如何解决名称“名称”和“值”的问题

如何解决:上下文提供程序未将新的上下文值传递给子级

在任何上下文中都找不到“商店”

反应错误:“在...的上下文中找不到'商店'”

在“ Connect(AppComponent)”的上下文中找不到“商店”

Debian:找不到deb命令。我该如何解决?

Unix - 我该如何解决:找不到命令

找不到与“<%”匹配的结束标记,我该如何解决?

如何修复“在DragSource(Component)的上下文中找不到拖放管理器”

在父级上下文中调用父级控制器的函数

在递归上下文中如何解释惰性?

我如何在cakephp 3的上下文中解决此SQLSTATE错误?

如何将OpenGL上下文放入我当前的线程中,找不到OpenGL上下文

在onclick事件的上下文中,“ this”如何工作?(JavaScript)

如何从静态上下文中解决非静态方法错误?

如何解决Razor Pages错误“ CS0103当前上下文中不存在名称'Json'”?