简体   繁体   中英

Cannot resolve method setText(java.lang.String) in Fragment

I am using the Pedometer to count the steps since the last reboot of a device.

I have created a textView which I want to set the text of to be the number of steps when the sensor is being used. (My TextView is called count)

My code for doing this is:

@Override
public void onSensorChanged(SensorEvent event) {
    if (activityRunning) {
        count.setText(String.valueOf(event.values[0]));
    }

I am getting an error here Cannot resolve method setText(java.lang.String)

I can post the rest of the class if it would help to fix the problem

Thanks

This is the rest of the class:

public class Tab3 extends Fragment implements SensorEventListener {

private SensorManager sensorManager;
private TextView textView;
boolean activityRunning;
private Button buttonReturn;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.tab3, container, false);

    TextView count = (TextView) getActivity().findViewById(R.id.count);

    sensorManager = (SensorManager) getActivity().getSystemService(Context.SENSOR_SERVICE);

    getActivity().getSystemService(Context.SENSOR_SERVICE);

    return rootView;
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
}

@Override
public void onResume() {
    super.onResume();
    activityRunning = true;
    Sensor countSensor = sensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
    if (countSensor != null) {
        sensorManager.registerListener(this, countSensor, SensorManager.SENSOR_DELAY_UI);
    } else {
        Toast.makeText(getActivity(), "Count sensor not available!", Toast.LENGTH_LONG).show();
    }

}

@Override
public void onPause() {
    super.onPause();
    activityRunning = false;
}

@Override
public void onSensorChanged(SensorEvent event) {
    if (activityRunning) {
        count.setText(String.valueOf(event.values[0]));
    }

}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {

}
}

This is the XML file associated with this class:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.application.placepicker.TabbedActivity$PlaceholderFragment">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_marginTop="114dp"
    android:gravity="center"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true">

    <TextView
        android:id="@+id/textLogin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="Step counter"
        android:textSize="40sp"
        android:textColor="@color/colorAccent"
        android:fontFamily="sans-serif"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="33dp"
        android:layout_gravity="center_horizontal"
        android:textSize="26dp"
        android:text="Steps walked so far today:"
        android:textColor="@color/colorAccent" />

    <TextView
        android:id="@+id/count"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dp"
        android:layout_marginLeft="50dp"
        android:text="---"
        android:textColor="@color/colorAccent"
        android:textSize="36dp" />

</LinearLayout>

Change

private TextView textView;

to

private TextView count;

And change

TextView count = (TextView) getActivity().findViewById(R.id.count);

to

count = (TextView) getActivity().findViewById(R.id.count);

Bring "count" out of "onCreateView", so it will be accessiable in "onSensorChanged"

public class Tab3 extends Fragment implements SensorEventListener {
 private SensorManager sensorManager;
 private TextView textView;
 boolean activityRunning;
 private Button buttonReturn;

private TextView count ;//<-- add this

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.tab3, container, false);

    //define count global 
    count = (TextView)rootView.findViewById(R.id.count);

    sensorManager = (SensorManager) getActivity().getSystemService(Context.SENSOR_SERVICE);

    getActivity().getSystemService(Context.SENSOR_SERVICE);

    return rootView;
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
}

@Override
public void onResume() {
    super.onResume();
    activityRunning = true;
    Sensor countSensor = sensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
    if (countSensor != null) {
        sensorManager.registerListener(this, countSensor, SensorManager.SENSOR_DELAY_UI);
    } else {
        Toast.makeText(getActivity(), "Count sensor not available!", Toast.LENGTH_LONG).show();
    }

}

@Override
public void onPause() {
    super.onPause();
    activityRunning = false;
}

@Override
public void onSensorChanged(SensorEvent event) {
    if (activityRunning) {
        count.setText(String.valueOf(event.values[0]));
    }

}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {

}
}

How did you define your "count"?

If we suppose that you has defined the TextView correctly, you will get error in null object, so it is better to change your code to below:

   count.setText(""+event.values[0]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM