0

ロード時にデータをテキストビューにロードしようとしています。私のアプリはフラグメントを使用し、3 つのタブがあります。最初の 2 つのタブにデータをロードすることはできますが、3 番目のタブにデータをロードしようとするとヌル ポインター例外が発生します。コードは次のとおりです。

public void readAndLoadDataFromFile()
{
    String[] fieldNamesInFile = new String[50];
    double[] valuesInFile = new double[50];
    // read all the data from file
    readAllDataFromFile(fieldNamesInFile, valuesInFile);

    // Tab 1
    double value1 = valuesInFile[Arrays.asList(fieldNamesInFile).indexOf("Field1")];
    // copy the data into the fields
    ((EditText) findViewById(R.id.editText1)).setText(String.valueOf(value1)); // OK

    // Tab 2
    double value2 = valuesInFile[Arrays.asList(fieldNamesInFile).indexOf("Field2")];
    // copy the data into the fields
    ((EditText) findViewById(R.id.editText2)).setText(String.valueOf(value2)); // OK

    // Tab 3
    double value3 = valuesInFile[Arrays.asList(fieldNamesInFile).indexOf("Field3")];
    // copy the data into the fields
    ((EditText) findViewById(R.id.editText3)).setText("123"); // null pointer exception


}

タブ 3 のレイアウト XML ファイルは次のとおりです (最初の 2 つのタブは類似しています)。

<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/myScrollView"
android:layout_width="fill_parent" 
android:layout_height="wrap_content">

<TableLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >


<TableRow
        android:id="@+id/tableRow3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp" >

        <TextView
            android:id="@+id/textView3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Text 3:"
            android:clickable="true"
            android:gravity="right"
            android:layout_weight="2"
            android:layout_marginRight="2dp"
            android:textAppearance="?android:attr/textAppearanceSmall" />

        <EditText
            android:id="@+id/editText3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:layout_weight="1"
            android:layout_marginLeft="2dp"
            android:ems="10"
            android:text="25"
            android:inputType="numberDecimal"
            android:textAppearance="?android:attr/textAppearanceSmall" />

</TableRow>
</TableLayout>

</ScrollView>

私は何が欠けていますか?

4

2 に答える 2

0

場合によっては、プロジェクトで単に R ファイルの再構築が必要になることがあります。Eclipse で「クリーン」オプションを試すことができます ([プロジェクト] -> [クリーン] を選択し、プロジェクトを選択します)。R ファイルをチェックして、エントリがあるかどうかを確認することもできます。R ファイルはgenディレクトリにあります。そうでない場合は、R ファイルを削除し、再コンパイル時に再構築して、表示されるかどうかを確認してみてください。

もう 1 つの確認事項は、アクセスされている R ファイルが Android R ファイルではないことを確認することです。そのためのインポートを確認するか、パッケージ名に設定され((EditText) findViewById(com.yourproject.app.R.id.editText3)).setText("123");た部分で行をに変更して強制します。com.yourproject.appそうすることで、正しい R ファイルを確実に取得できます。

于 2013-03-15T00:53:35.623 に答える
0

ナビゲーションを強制的に特定のタブに移動させ、ビューをロードすることで、問題を修正 (ハッキング) しました。

ActionBar actionBar = getActionBar();
actionBar.setSelectedNavigationItem(2); // go to Tab 3

double value3 = valuesInFile[Arrays.asList(fieldNamesInFile).indexOf("Field3")];
// copy the data into the fields
((EditText) findViewById(R.id.editText3)).setText("123"); // no more null pointer exception

自慢ではありませんが、少なくとも今のところ問題は解決しています。

于 2013-03-15T20:53:10.270 に答える