2016-12-10 8 views
1

今日、私は自分のアプリケーションの1つをテストとして純粋なkotlinビルドに翻訳しています。私は現在、アレイアダプターに苦しんでおり、このエラーが発生しています。Anja DSL with ArrayAdapter

File from xml type layout resource ID #0x7f0c000a 

これは私の活動です。エラーが発生しました。

class KotlinTest : Activity() { 

    override fun onCreate(savedInstanceState: Bundle?) { 
     super.onCreate(savedInstanceState) 

     val arr = Array(5, Int::toString) 

     verticalLayout { 


      id = R.id.test_text_layout 

      textView { 
       width = matchParent 
       height = matchParent 
       id = R.id.test_text_item 
      } 


      listView { 
       id = R.id.test_text_view 
      } 
     } 

     val cardAdapter: ArrayAdapter<String>? = ArrayAdapter(ctx, R.id.test_text_layout, arr) 

     val tempView: ListView = findViewById(R.id.test_text_view) as ListView 

     tempView.adapter = cardAdapter 

     cardAdapter?.notifyDataSetChanged() 

    } 

} 

これはids.xmlファイルです。

<?xml version="1.0" encoding="utf-8"?> 
<resources> 

    <item name="test_text_item" type="id"/> 
    <item name="test_text_view" type="id"/> 
    <item name="test_text_layout" type="id"/> 

</resources> 

私はいくつかのことを試みましたが運がないので、どんな指針も認められます。私はちょうど何かばかげたことがないと思う。

+0

を拡張する必要があり、エラーが表示されていますか? – voddan

+0

@voddan 'tempView.adapter = cardAdapter'は、実際にarrayAdapterをビルドしようとしていて、レイアウトを膨張しようとするまではエラーが出ませんが、間違っている可能性があります。私がその行とそれを実行し、それ以下の行をコメントアウトした場合、私は空白の活動をします。 – thechucklingatom

答えて

2

問題の原因

val cardAdapter = ArrayAdapter(ctx, R.layout.test_text_layout, arr) 

活動のコードでtest_text_layout.xmlそれを呼び出すと、以下の内容

<?xml version="1.0" encoding="utf-8"?> 
<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"/> 

を追加し、res/layoutディレクトリにファイルを作成します<item name="test_text_layout" type="id"/>

この行を削除する - あなたは間違って渡されましたパラメータ。 documentation

リソースint型から:ビューをインスタンス化するときに使用するのTextViewを含むレイアウトファイルのリソースID。

あなたは完全にXMLを取り除きたい場合は、あなたがどのラインでArrayAdapter

+0

そして、それをAnkoによって生成されたカスタムレイアウトにリンクする方法はありません。それは私がそれを調べたほど、私が推測し始めたものです。私が行っているアプリケーションのように見えますが、ArrayAdapterを拡張する必要があります。これを確認していただきありがとうございます。 – thechucklingatom

+0

@chucklingatomヘルプが必要な場合はチャットで私にpingしてください。http://chat.stackoverflow.com/rooms/26424/iosandroidchaosoverflow –