2017-03-28 5 views
-1

フラグメントのサンプルアプリケーションを作成しています。 フラグメントを作成し、それからビューをレンダリングしました。 しかし、レイアウトでandroid:id="@+id/text2"を使用すると、ビューをレンダリングしようとするとアプリケーションがクラッシュします。FindviewByIdは "+ id"を使用するとnullを返しますが、Android:idを使用している場合はnullを返します

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/content" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

<RelativeLayout 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

<TextView android:id="@+id/text2" 
    style="?android:textAppearanceLarge" 
    android:textStyle="bold" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginBottom="16dp" /> 

<EditText 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_below ="@id/text2" 
    android:inputType="textPersonName" 
    android:text="FIR Number" 
    android:ems="10" 
    android:layout_centerHorizontal="true" 
    android:id="@+id/editText" 
    tools:layout_marginTop="10dp" 
    style="@android:style/Widget.EditText" 
    android:textAlignment="center" /> 


</RelativeLayout> 

</ScrollView> 

しかし、私はandroid:id="@android:id/text2"を使用する場合、それがうまく機能し、

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/content" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<RelativeLayout 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<TextView android:id="@android:id/text2" 
    style="?android:textAppearanceLarge" 
    android:textStyle="bold" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginBottom="16dp" /> 

<EditText 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_below ="@android:id/text2" 
    android:inputType="textPersonName" 
    android:text="FIR Number" 
    android:ems="10" 
    android:layout_centerHorizontal="true" 
    android:id="@+id/editText" 
    tools:layout_marginTop="10dp" 
    style="@android:style/Widget.EditText" 
    android:textAlignment="center" /> 

</RelativeLayout> 

</ScrollView> 

フラグメントビューのコードは両方のケースのために変更されていません。

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
{ 
// Inflate the layout containing a title and body text. 
    ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_accident_details_step1, container, false); 

    // Set the title view to show the page number. 
    ((TextView) rootView.findViewById(android.R.id.text2)).setText(getString(R.string.title_template_step, mPageNumber + 1)); 
    return rootView; 
} 

なぜ1つのケースで動作し、なぜ他のケースで動作しないのですか?誰でも助けてくれますか?

+1

にandroid.Rは間​​違っています。それはあなたのプロジェクトで作成するものではなく、アンドロイドパッケージのIDを呼び出します。ので、 'R'の前に 'アンドロイド'を削除 –

答えて

1

あなたはとてもあなたがフラグメントにAndroidのIDを使用している

((TextView) rootView.findViewById(R.id.text2)).setText(getString(R.string.title_template_step, mPageNumber + 1)); 
//        ^^^^^^^^^^ 
+0

ありがとうトン。これは確かに助けになるでしょう。 –

0

を使用し、@+id/を使用してRファイル 内部で生成されたIDにポイントにR.id.text2を使用Syntax docs続きを読むし、また、約Accessing Platform Resources

を読む必要があります。

((TextView) rootView.findViewById(android.R.id.text2)).setText(getString(R.string.title_template_step, mPageNumber + 1)); 

変更

((TextView) rootView.findViewById(R.id.text2)).setText(getString(R.string.title_template_step, mPageNumber + 1)); 
関連する問題