2017-07-28 6 views
0

私はアプリケーションを開発していると私は、フラグメントに私のアプリがクラッシュしたテキストのサイズを設定する
テキストサイズを動的

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

を使用するときの設定時にアプリケーションがクラッシュ。また、findViewById()にこの警告が表示されます。

method invocation findviewbyid may produce java.lang.nullpointerexception 

私は @SuppressWarnings("ConstantConditions")することにより、この警告を抑制しようとしましたが、そうすることに成功したが、これは私のアプリがクラッシュから救われるために支援されていません。
それでもクラッシュします。

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> 

いくつかの点に注意する:ここでは
は、Javaコードだ

  • をプログラムでテキストまたはテキストサイズを設定せずに、それが正常に動作します。
  • onCreateView()onViewCreated()にすべてのテキストを入れようとしましたが、それはSuppressWarningsですが、何も処理されませんでした。
  • 私はすべてandroid:textをXMLから削除しようとしましたが効果はありません。
  • java内のすべてsetTextSizeを削除した後にアプリを実行しようとしましたが、クラッシュしました。
  • また、Javaファイルからすべてのnullableを削除しようとしましたが、Method Invocation警告がまだ残っていて、それも役立たなかった。

私が達成したいのは、各デバイスに応じてテキストサイズを設定したいということです。

+0

あなたが貼り付けることができますエラーログ –

+0

あなたはlogcatを投稿できますか? – Reena

答えて

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

私が気づいたことは、同じ文字列を使用していたのですが、それらを変更することはどちらも役に立たなかったことです。 –

+0

私は2つの異なるタブで2つの方法を試しました.1つはあなたのもので、もう1つはonViewCreatedでSuppressWarningsを使用し、onViewCreatedでこれらのテキスト行を貼り付けることで、両方が個々のタブで機能していることを確認することです。ありがとうございます –

+0

あなたは大歓迎です! –

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

それは最終的に働いていますが、SuppressWarningsをonViewCreatedに設定しましたが、心配はありません。ありがとうございました。 YoursとBosco sabinのメソッドは、2つの個別のメニュータブで動作しています。 –

+0

onViewCreated()内のアイテムを表示しようとするため、getView()のために移動する必要はありません。 –

+0

これを思い出します。ありがとうございました –

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); 
関連する問題