/ / Android-ListActivity、ヘッダーとフッターのビューを追加-Android、ヘッダー、フッター、リストアクティビティ

Android - ListActivity、ヘッダーとフッターの表示を追加 - Android、ヘッダー、フッター、listactivity

ListActivity、listviewを使用しています。

listView = getListView();

ちょうど完璧に働いています。私はフッタービューを追加しました

LayoutInflater inflater = getLayoutInflater();
listView.addFooterView( inflater.inflate( R.layout.footer, null ), null, false);

すべてが光沢がありますが醜いので、このフッタービュー(edittextが1つとbuttonが1つだけ)をlistViewのヘッダーに追加したかったので

LayoutInflater inflater = getLayoutInflater();
listView.addHeaderView( inflater.inflate( R.layout.footer, null ), null, false);

突然、すべてがうまくいかなくなり、すぐにRuntimeExceptionが発生します。

Suspended(exception RuntimeException)
ActivityThread.performLaunchActivity(ActivityThread$ActivityRecord, Intent)
ActivityThread.handleLaunchActivity(ActivityThread$ActivityRecord, Intent)
ActivityThread.access$2200(ActivityThread, Activity$ActiviyRecord, Intent),
so on..

なぜ例外が発生するのですか? addFooterViewとaddHeaderViewの違い、およびListActivityにヘッダーを追加するにはどうすればよいですか?

更新

コメントで読むことができるように、私のlogcatはまだ機能しませんが、私は今すぐ次に試してみました:

} catch(Exception e){
Writer result = new StringWriter();
PrintWriter printWriter = new PrintWriter(result);
e.printStackTrace(printWriter);
String error = result.toString();
}

その後、ブレークポイントを設定し、式セクションでエラーを読み取ることができます。と言いました :

java.lang.IllegalStateException: Cannot add header view to list -- setAdapter has already been called.

それは私たち全員にとって有益でした。コマンドの種類を変更すると、正常に機能します。

回答:

回答№1の11

ログとして

java.lang.IllegalStateException:ヘッダービューをリストに追加できません- setAdapterはすでに呼び出されています。

Listviewのメソッド addHeaderView または addFooterView 前に呼び出す必要があります setAdapter.


回答№2の場合は0

ヘッダーとフッターのレイアウトxmlを作成する

header_layout.xml

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="header"
/>
</RelativeLayout>

footer_layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="footer"
/>
</RelativeLayout>

現在アクティビティjavaファイルのonCreate()メソッドで追加

listView = (ListView) findViewById(R.id.listView1);
LayoutInflater inflater = getLayoutInflater();
ViewGroup header = (ViewGroup) inflater.inflate(R.layout.header, listView,
false);
ViewGroup footer = (ViewGroup) inflater.inflate(R.layout.footer, listView,
false);
listView.addHeaderView(header, null, false);
listView.addFooterView(footer, null, false);
listView.setAdapter(adapter);

回答№3の-6

そのため、listViewにヘッダービューを追加する場合は、setListAdapter()を使用する前に厳密に行う必要があります。そうしないと、IllegalStateExceptionが発生します。