I am new to programming in Android Studio and I got stuck on this error, I cannot find an answer anywhere, so maybe someone can help me. The error I got is:
error: incompatible types: View cannot be converted to Chronometer
Chronometer.java
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class Chronometer extends AppCompatActivity {
Chronometer chronometer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chronometer);
chronometer = (Chronometer) findViewById(R.id.chronometerID);
}
}
Your class extends AppCompatActivity. AppCompatActivity does not extend View. findViewById
returns a View. Therefore this does not work. You cannot convert between types that are in different hierarchies.
Why are you trying to create an instance of your Activity within itself anyway? Generally you would just want to override the lifecycle methods.
Chronometer widget class name clashes with your activity name. You can rename your activity or use full name for you widget:
android.widget.Chronometer chronometer;
chronometer = (android.widget.Chronometer) findViewById(R.id.chronometerID);
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.