Ошибка при запуске приложения на android
Вылетает такая ошибка:
Process: com.example.recview2, PID: 10144
android.view.InflateException:
Binary XML file line #23 in com.example.recview2:layout/list_item: Binary XML file line #23 in com.example.recview2:layout/list_item: Error inflating class TextViewCaused by: android.view.InflateException: Binary XML file line #23 in com.example.recview2:layout/list_item: Error inflating class TextView
Caused by: java.lang.UnsupportedOperationException:
Can't convert value at index 93 to dimension: type=0x12, theme={InheritanceMap=[id=0x7f100275com.example.recview2:style/Theme.RecView2, id=0x7f100078com.example.recview2:style/Base.Theme.RecView2, id=0x7f100236com.example.recview2:style/Theme.Material3.DayNight.NoActionBar, id=0x7f100244com.example.recview2:style/Theme.Material3.Light.NoActionBar, id=0x7f10023ecom.example.recview2:style/Theme.Material3.Light, id=0x7f10005fcom.example.recview2:style/Base.Theme.Material3.Light, id=0x7f1000b4com.example.recview2:style/Base.V24.Theme.Material3.Light, id=0x7f10008ecom.example.recview2:style/Base.V14.Theme.Material3.Light, id=0x7f100263com.example.recview2:style/Theme.MaterialComponents.Light, id=0x7f10006ecom.example.recview2:style/Base.Theme.MaterialComponents.Light, id=0x7f1000a8com.example.recview2:style/Base.V21.Theme.MaterialComponents.Light, id=0x7f100096com.example.recview2:style/Base.V14.Theme.MaterialComponents.Light, id=0x7f100097com.example.recview2:style/Base.V14.Theme.MaterialComponents.Light.Bridge, id=0x7f10013bcom.example.recview2:style/Platform.MaterialComponents.Light, id=0x7f10021acom.example.recview2:style/Theme.AppCompat.Light, id=0x7f100052com.example.recview2:style/Base.Theme.AppCompat.Light, id=0x7f1000bacom.example.recview2:style/Base.V28.Theme.AppCompat.Light, id=0x7f1000b7com.example.recview2:style/Base.V26.Theme.AppCompat.Light, id=0x7f1000b1com.example.recview2:style/Base.V23.Theme.AppCompat.Light, id=0x7f1000afcom.example.recview2:style/Base.V22.Theme.AppCompat.Light, id=0x7f1000a4com.example.recview2:style/Base.V21.Theme.AppCompat.Light, id=0x7f1000bdcom.example.recview2:style/Base.V7.Theme.AppCompat.Light, id=0x7f100138com.example.recview2:style/Platform.AppCompat.Light, id=0x7f100143com.example.recview2:style/Platform.V25.AppCompat.Light, id=0x1030241android:style/Theme.Material.Light.NoActionBar, id=0x1030237android:style/Theme.Material.Light, id=0x103000candroid:style/Theme.Light, id=0x1030005android:style/Theme], Themes=[com.example.recview2:style/Theme.RecView2, forced, com.example.recview2:style/Theme.AppCompat.Empty, forced, android:style/Theme.DeviceDefault.Light.DarkActionBar, forced]}
at android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java:795)
При этом в коде ошибок не вижу.
Код активити activity_main
с RecyclerView
:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recView">
</androidx.recyclerview.widget.RecyclerView>
</androidx.constraintlayout.widget.ConstraintLayout>
Код элемента (Айтема) list_item.xml
:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ivan Ivanov"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:textSize="20sp"
android:id="@+id/fullname_txt"
></TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="8class"
android:lastBaselineToBottomHeight="@+id/fullname_txt"
android:layout_below="@id/fullname_txt"
android:layout_marginLeft="10dp"
android:id="@+id/clas_txt">
</TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_below="@id/fullname_txt"
android:layout_marginLeft="10dp"
android:textSize="27sp"
android:id="@+id/skills_txt"
>
</TextView>
</RelativeLayout>
Код адаптера:
package com.example.recview2;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
public class StudentAdapter extends RecyclerView.Adapter<StudentAdapter.ViewHolder> {
private LayoutInflater inflater;
private List<Student> students;
StudentAdapter(Context context, List<Student> students) {
this.students = students;
this.inflater = LayoutInflater.from(context);
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.list_item, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull StudentAdapter.ViewHolder holder, int position) {
Student student = students.get(position);
holder.fullName.setText(student.getFullname());
holder.clas.setText(Integer.toString(student.getclas()));
holder.skills.setText(Integer.toString(student.getskills()));
}
@Override
public int getItemCount() {
return students.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
TextView clas, fullName, skills;
public ViewHolder(@NonNull View view) {
super(view);
fullName = view.findViewById(R.id.fullname_txt);
clas = view.findViewById(R.id.clas_txt);
skills = view.findViewById(R.id.skills_txt);
}
}
}
Код MainActivity.java
:
package com.example.recview2;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private RecyclerView recView;
private ArrayList<Student> students = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recView = findViewById(R.id.recView);
inizilazieData();
StudentAdapter studentAdapter = new StudentAdapter(this, students);
recView.setLayoutManager(new LinearLayoutManager(this));
recView.setAdapter(studentAdapter);
}
public void inizilazieData() {
students.add(new Student("Iban Ivanov", 1, 2));
students.add(new Student("Petro Ivanov", 1, 2));
students.add(new Student("Vannn Ivanov", 1, 2));
students.add(new Student("bb Ivanov", 1, 2));
}
}
При этом все проверил, ошибок нигде нет, но приложение не запускается и вылетает.
В чем может быть дело?
Ответы (1 шт):
Нужно изменить атрибут android:lastBaselineToBottomHeight
. Ему нужно назначить некоторую величину, равную расстоянию от базовой линии последней строки до нижнего края RelativeLayout
, а не идентификатор вышестоящего элемента.
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="8class"
android:lastBaselineToBottomHeight="30dp"
android:layout_below="@id/fullname_txt"
android:layout_marginLeft="10dp"
android:id="@+id/clas_txt">