1

このソースをAndroidSDKのAlertDialogres\layoutフォルダーのレイアウトとして見ました。

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout android:id="@+id/body"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:baselineAligned="false"
        android:paddingLeft="8dip"
        android:paddingTop="10dip"
        android:paddingRight="8dip"
        android:paddingBottom="10dip">

        <ProgressBar android:id="@android:id/progress"
            style="@android:style/Widget.ProgressBar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:max="10000"
            android:layout_marginRight="12dip" />

        <TextView android:id="@+id/message"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical" />
    </LinearLayout>
</FrameLayout>

IDが次のような「メッセージ」であるTextViewを見つけようとしました。

TextView tvMessage = (TextView)dialog.findViewById(android.R.id.message);

正常に動作します。次に、IDが次のように「body」であるLinearLayoutを見つけようとしました。

LinearLayout body = (LinearLayout)dialog.findViewById(android.R.id.body);

しかし、日食はこのエラーを示しています:

body cannot be resolved or is not a field

これはスニペットコードです:

ProgressDialog dialog = new ProgressDialog(WriteOpinionActivity.this);
            dialog.setMessage("some text");
dialog.show();
TextView tvMessage = (TextView)dialog.findViewById(android.R.id.message);
LinearLayout body = (LinearLayout)dialog.findViewById(android.R.id.body);

このエラーが発生する理由と、「本体」に到達する方法を教えてください。

4

2 に答える 2

0

代わりにこれを使用してください。

TextView tvMessage = (TextView)dialog.findViewById(R.id.message);  
LinearLayout body = (LinearLayout)dialog.findViewById(R.id.body);  

そもそも実際には使っていませんでしandroid.R.id.messageた。@+id/message。という名前の新しいidリソースを作成していることを意味しますmessage

Androidフレームワークに存在するを使用するidには、ここで使用していることがわかります@android:id/progress。そして、ソースでは、あなたはそれを持っていたようにそれを使用します:android.R.id.progress

それ以外の場合はandroid:id="@=id/myIdentifier"、XMLおよび
findViewById(R.id.myIdentifier)コードで使用します。

http://developer.android.com/guide/topics/resources/overview.html

于 2013-03-27T02:24:53.243 に答える
0

res基本的に、プラットフォームフォルダからすべてのリソースにアクセスできるわけではありません。

たとえば@+id/message、 (API 17の場合)で見つけることができるので、で定義されたものをglobal_actions_item.xml, usb_storage_activity.xml, alert_dialog_holo.xml, js_prompt.xml, progress_dialog_holo.xml, progress_dialog.xml, alert_dialog.xml取得する保証はありません(別のファイルで定義でき、同じ名前であるため、進行中のダイアログで見つけることができます) )。idprogress_dialog.xml

実際には、パブリックリソースはここで定義されていますが、かなり古くなっているように見えるため、一部のリソースが欠落している可能性があります。アクセス可能なすべてのリソースのリストに関する次の質問を確認してください:1および2

于 2013-03-27T03:32:25.367 に答える