0

グリッド ビューがあり、グリッドに表示する必要がある 6 つの項目 (文字列) の配列を定義しました。

Grid_ViewActivity:

public class Grid_ViewActivity extends Activity {
    /** Called when the activity is first created. */
      //TextView selection;
      String[] characters=getResources().getStringArray(R.array.characters);

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        GridView gv=(GridView)findViewById(R.id.gridView1);
        //selection=(TextView)findViewById(R.id.selection);
        ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,R.layout.simple_list_item,characters);
        gv.setAdapter(adapter);
        gv.setOnItemClickListener(new OnItemClickListener(){



      @Override
      public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            //selection.setText(characters[position]);
            Toast.makeText(getApplicationContext(),
                ((TextView) v).getText()  , Toast.LENGTH_SHORT).show();

      }
        });
}
    }

main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

<GridView
android:id="@+id/gridView1"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:numColumns="auto_fit"
android:verticalSpacing="20px"
android:horizontalSpacing="5px"
android:gravity="center"
android:columnWidth="40dp"
android:stretchMode="columnWidth"

/>
</LinearLayout>

simple_listitrm.xml:

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
  <TextView 
      android:id="@+id/tv1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

    android:textSize="14pt"
    android:textStyle="bold"
    />
</LinearLayout>

null ポイントの例外が発生しました。これを実行すると、アプリケーションが強制的に閉じられます。どこが間違っていますか?

4

4 に答える 4

0

ArrayAdapter では、リソース ID が TextView XML 例外である必要があります。これは、ArrayAdapter が期待するものを提供していないことを意味します。このコンストラクターを使用する場合:

 new ArrayAdapter<String>(this, R.layout.a_layout_file, this.file)

R.Layout.a_layout_file は、TextView のみを含む xml レイアウト ファイルの ID である必要があります (TextView は、LinearLayout、RelativeLayout などの別のレイアウトでラップすることはできません)。

このようなもの:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" 
// other attributes of the TextView
/>

リスト行のレイアウトを少し変更したい場合は、単純な TextView ウィジェットで次のコンストラクターを使用します。

    new ArrayAdapter<String>(this, R.layout.a_layout_file, 
    R.id.the_id_of_a_textview_from_the_layout, this.file)

さまざまなビューを含めることができるレイアウトの id を指定しますが、ArrayAdapter に渡す id (3 番目のパラメーター) を持つ TextView を含める必要があるため、行レイアウトのどこに文字列を配置するかを知ることができます。

于 2013-07-03T05:40:13.093 に答える
0

変更する必要があります:

  @Override
  public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
        //selection.setText(characters[position]);
        Toast.makeText(getApplicationContext(),
            characters[position], Toast.LENGTH_SHORT).show();

  }
    });
于 2013-07-03T05:51:36.330 に答える
0

以下のコードを使用してみてください。

simple_listitrm.xml をこれに変更します。

oncreate をこれに変更します。

   @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    characters = getResources().getStringArray(R.array.months);
    GridView gv = (GridView) findViewById(R.id.gridView1);
    // selection=(TextView)findViewById(R.id.selection);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            R.layout.test, characters);
    gv.setAdapter(adapter);
    gv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View v,
                int position, long id) {
            // selection.setText(characters[position]);
            Toast.makeText(getApplicationContext(),
                    ((TextView) v).getText(), Toast.LENGTH_SHORT).show();

        }
    });
}
于 2013-07-02T12:16:03.563 に答える