动态设置文字大小时应用崩溃

时间:2017-07-28 10:19:07

标签: android nullpointerexception

我正在开发一个应用程序,每当我使用

text1.setTextSize(10 * getResources().getDisplayMetrics().density);

在片段中设置文本大小,我的应用程序崩溃了。 findViewById()也显示此警告。

method invocation findviewbyid may produce java.lang.nullpointerexception

我试图通过@SuppressWarnings("ConstantConditions")取消此警告并成功完成此操作,但这并没有帮助我的应用程序免于崩溃。
它仍然崩溃。
这是Java代码:

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.TextView;

public class Metro extends Fragment {

    @SuppressWarnings("ConstantConditions")
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        TextView text1 = getView().findViewById(R.id.textView1);
        text1.setText(R.string.route);
        text1.setTextSize(10 * getResources().getDisplayMetrics().density);
        TextView text2 = getView().findViewById(R.id.textView2);
        text2.setText(R.string.map);
        text2.setTextSize(10 * getResources().getDisplayMetrics().density);
        TextView text3 = getView().findViewById(R.id.textView3);
        text3.setText(R.string.location);
        text3.setTextSize(10 * getResources().getDisplayMetrics().density);
        TextView text4 = getView().findViewById(R.id.textView4);
        text4.setText(R.string.about);
        text4.setTextSize(10 * getResources().getDisplayMetrics().density);

        return inflater.inflate(R.layout.fragment_metro, container, false);
    }
}

XML的代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    android:orientation="vertical"
    android:background="@drawable/home"
    xmlns:android="http://schemas.android.com/apk/res/android" >

    <android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dp"
        android:id="@+id/button1"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/button2"
        app:layout_constraintLeft_toLeftOf="parent">

        <ImageView
            android:id="@+id/imageview1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/maps"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toBottomOf="@+id/imageview1"
            app:layout_constraintLeft_toLeftOf="@+id/imageview1"
            app:layout_constraintRight_toRightOf="@+id/imageview1"
            android:id="@+id/textView1"
            android:text="@string/route"/>

    </android.support.constraint.ConstraintLayout>

    <android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button2"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toRightOf="@+id/button1"
        android:layout_marginTop="40dp">

        <ImageView
            android:id="@+id/imageview2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/maps"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/textView2"
            app:layout_constraintTop_toBottomOf="@+id/imageview2"
            app:layout_constraintLeft_toLeftOf="@+id/imageview2"
            app:layout_constraintRight_toRightOf="@+id/imageview2"
            android:text="@string/map"/>

    </android.support.constraint.ConstraintLayout>

    <android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button3"
        app:layout_constraintTop_toBottomOf="@+id/button1"
        app:layout_constraintLeft_toLeftOf="@+id/button1"
        app:layout_constraintRight_toRightOf="@+id/button1"
        android:layout_marginTop="20dp">

        <ImageView
            android:id="@+id/imageview3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/maps"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/textView3"
            app:layout_constraintTop_toBottomOf="@+id/imageview3"
            app:layout_constraintLeft_toLeftOf="@+id/imageview3"
            app:layout_constraintRight_toRightOf="@+id/imageview3"
            android:text="@string/location"/>

    </android.support.constraint.ConstraintLayout>

    <android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button4"
        app:layout_constraintTop_toBottomOf="@+id/button2"
        app:layout_constraintLeft_toLeftOf="@+id/button2"
        app:layout_constraintRight_toRightOf="@+id/button2"
        android:layout_marginTop="20dp">

        <ImageView
            android:id="@+id/imageview4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/maps"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/about"
            android:id="@+id/textView4"
            app:layout_constraintTop_toBottomOf="@+id/imageview4"
            app:layout_constraintLeft_toLeftOf="@+id/imageview4"
            app:layout_constraintRight_toRightOf="@+id/imageview4"/>

    </android.support.constraint.ConstraintLayout>

</android.support.constraint.ConstraintLayout>

有些注意事项:

  • 如果不以编程方式设置文本或文本大小,则可以正常工作。
  • 我尝试将所有这些文本放在onCreateView()onViewCreated()SuppressWarnings,但没有任何效果。
  • 我尝试从XML中删除所有android:text但没有效果。
  • 我已经尝试在删除java中的所有setTextSize后运行应用程序,但它仍然崩溃。
  • 我还试图从Java文件中删除所有的nullables,但是方法调用警告仍然在那里,它也没有帮助。

我想要实现的是我想根据每个设备设置文本大小。

3 个答案:

答案 0 :(得分:1)

 public class Metro extends Fragment {
@SuppressWarnings("ConstantConditions")
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    View view=inflater.inflate(R.layout.fragment_metro, container, false);
    TextView text1 =(TextView) view.findViewById(R.id.textView1);
    text1.setText(getString(R.string.route));
    text1.setTextSize(10 * getResources().getDisplayMetrics().density);
    TextView text2 = (TextView) view.findViewById(R.id.textView2);
    text2.setText(getString(R.string.route));
    text2.setTextSize(10 * getResources().getDisplayMetrics().density);
    TextView text3 = (TextView) view.findViewById(R.id.textView3);
    text3.setText(getString(.string.route));
    text3.setTextSize(10 * getResources().getDisplayMetrics().density);
    TextView text4 = (TextView) view.findViewById(R.id.textView4);
    text4.setText(getString(R.string.route));
    text4.setTextSize(10 * getResources().getDisplayMetrics().density);


    return view;
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

}

}

答案 1 :(得分:1)

试试这个:

public class Metro extends Fragment {

    @SuppressWarnings("ConstantConditions")
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_metro, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        TextView text1 = view.findViewById(R.id.textView1);
        text1.setText(R.string.route);
        text1.setTextSize(10 * getResources().getDisplayMetrics().density);

        TextView text2 = view.findViewById(R.id.textView2);
        text2.setText(R.string.route);
        text2.setTextSize(10 * getResources().getDisplayMetrics().density);

        TextView text3 = view.findViewById(R.id.textView3);
        text3.setText(R.string.route);
        text3.setTextSize(10 * getResources().getDisplayMetrics().density);

        TextView text4 = view.findViewById(R.id.textView4);
        text4.setText(R.string.route);
        text4.setTextSize(10 * getResources().getDisplayMetrics().density);
    }
}

答案 2 :(得分:1)

而不是这一行:

TextView text1 = getView().findViewById(R.id.textView1);

试试这个:

View view = inflater.inflate(R.layout.fragment_metro, container, false);
TextView text1 = view.findViewById(R.id.textView1);