如何修复“ java.lang.NumberFormatException:无效的浮点数:“ 16.0dip””

时间:2019-07-09 10:18:10

标签: android android-debug

我正在将我的BooksAdapter中的数据解析为BookDetailActivity,并且在logcat中出现NumberFormatException: Invalid float: 16.0dip错误。

我已经在线搜索了错误,但只有3个搜索结果没有帮助。

'BookDetailActivity'
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_book_detail);

    Book book = getIntent().getParcelableExtra("Book");
}

'BooksAdapter'
@Override
    public void onClick(View v) {
        int position = getAdapterPosition();
        Book selectedBook = books.get(position);
        Intent intent = new Intent(v.getContext(), BookDetail.class);
        intent.putExtra("Book", selectedBook);
        v.getContext().startActivity(intent);
    }

我希望用户单击RecyclerView中的项目后启动BookDetailActivity。而是,应用崩溃了。

(java;no:11 from logcat) = setContentView(R.layout.activity_book_detail);

日志猫出现的错误是:

  

java.lang.RuntimeException:无法启动活动ComponentInfo {www.frank.books / www.frank.books.BookDetail}:java.lang.NumberFormatException:无效的浮点数:“ 16.0dip”           在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2429)           在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493)           在android.app.ActivityThread.access $ 800(ActivityThread.java:166)           在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1283)           在android.os.Handler.dispatchMessage(Handler.java:102)           在android.os.Looper.loop(Looper.java:136)           在android.app.ActivityThread.main(ActivityThread.java:5590)           在java.lang.reflect.Method.invokeNative(本机方法)           在java.lang.reflect.Method.invoke(Method.java:515)           在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1280)           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1096)           在dalvik.system.NativeStart.main(本机方法)        原因:java.lang.NumberFormatException:无效的浮点数:“ 16.0dip”           在java.lang.StringToReal.invalidReal(StringToReal.java:63)           在java.lang.StringToReal.parseFloat(StringToReal.java:310)           在java.lang.Float.parseFloat(Float.java:300)           在android.content.res.TypedArray.getFloat(TypedArray.java:300)           在android.support.constraint.ConstraintLayout $ LayoutParams。(ConstraintLayout.java:2822)           在android.support.constraint.ConstraintLayout.generateLayoutParams(ConstraintLayout.java:1972)           在android.support.constraint.ConstraintLayout.generateLayoutParams(ConstraintLayout.java:482)           在android.view.LayoutInflater.rInflate(LayoutInflater.java:770)           在android.view.LayoutInflater.inflate(LayoutInflater.java:499)           在android.view.LayoutInflater.inflate(LayoutInflater.java:398)           在android.view.LayoutInflater.inflate(LayoutInflater.java:354)           在android.support.v7.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)           在android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)           在www.frank.books.BookDetail.onCreate(BookDetail.java:11)           在android.app.Activity.performCreate(Activity.java:5447)           在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)           在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2393)           在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493)           在android.app.ActivityThread.access $ 800(ActivityThread.java:166)           在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1283)           在android.os.Handler.dispatchMessage(Handler.java:102)           在android.os.Looper.loop(Looper.java:136)           在android.app.ActivityThread.main(ActivityThread.java:5590)           在java.lang.reflect.Method.invokeNative(本机方法)           在java.lang.reflect.Method.invoke(Method.java:515)           在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1280)           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1096)           在dalvik.system.NativeStart.main(本机方法)

  [1]: https://i.stack.imgur.com/DZoCS.png


book_detail.xml

    <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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=".BookDetail">

    <ImageView
        android:id="@+id/imgCover"
        android:layout_width="163dp"
        android:layout_height="184dp"
        android:layout_marginTop="@dimen/standard_margin"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tvAuthors"
        app:srcCompat="@drawable/book_open"
        android:contentDescription="@string/todo"
        android:layout_marginStart="@dimen/standard_margin"/>

    <TextView
        android:id="@+id/tvTitle"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/standard_margin"
        android:layout_marginTop="@dimen/standard_margin"
        android:layout_marginEnd="@dimen/standard_margin"
        android:text="@string/tv_title"
        android:textSize="36sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/tvAuthors"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/standard_margin"
        android:layout_marginTop="@dimen/standard_margin"
        android:layout_marginEnd="@dimen/standard_margin"
        android:text="@string/tv_subtitle"
        android:textSize="24sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tvTitle" />

    <TextView
        android:id="@+id/tv_authors"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/standard_margin"
        android:layout_marginTop="@dimen/standard_margin"
        android:text="@string/tv_author"
        android:textSize="18sp"
        app:layout_constraintStart_toEndOf="@+id/imgCover"
        app:layout_constraintTop_toBottomOf="@+id/tvAuthors" />

    <TextView
        android:id="@+id/tvPublisher"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/standard_margin"
        android:layout_marginTop="@dimen/standard_margin"
        android:text="@string/tv_publisher"
        android:textSize="16sp"
        app:layout_constraintStart_toEndOf="@+id/imgCover"
        app:layout_constraintTop_toBottomOf="@+id/tv_authors" />

    <TextView
        android:id="@+id/tvPublishedDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/standard_margin"
        android:layout_marginTop="@dimen/standard_margin"
        android:text="@string/tv_pub_date"
        android:textSize="16sp"
        app:layout_constraintStart_toEndOf="@+id/imgCover"
        app:layout_constraintTop_toBottomOf="@+id/tvPublisher" />

    <TextView
        android:id="@+id/textView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:layout_marginStart="@dimen/standard_margin"
        android:layout_marginTop="@dimen/standard_margin"
        android:layout_marginEnd="@dimen/standard_margin"
        android:text="@string/tv_detail"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="@dimen/standard_margin"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imgCover" />
</android.support.constraint.ConstraintLayout>

dimens.xml

    <?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="title_size">20sp</dimen>
    <dimen name="authors_size">18sp</dimen>
    <dimen name="published_date_padding">8dp</dimen>
    <dimen name="standard_margin">16dp</dimen>
</resources>

1 个答案:

答案 0 :(得分:1)

结帐此路线: at java.lang.StringToReal.invalidReal(StringToReal.java:63)表示第63行的类StringToReal有问题。

我认为您已将值为16.0dp的字符串强制转换为浮动值,这是无效的。

首先使用类似以下方法清除其中的dp

String a = "16.0dp";
Strign newA = a.replace("dp", "");

然后尝试投射它。

但是,由于这不是您要更改的来源,因此应检查在何处提供了此输出。检查您的dimens.xml并查看在哪里使用了无效值。