1

csipsimple アプリを編集していて、ContentView をこのファイル in_call_main.xml に設定しました。

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

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/mainFrame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ViewStub
        android:id="@+id/dropZones"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout="@layout/in_call_drop_zones" />

    <LinearLayout
        android:id="@+id/inCallContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dip"
            android:layout_weight="3"
            android:orientation="horizontal" >

            <!--
                Can we do better than using two linear layouts here?
                The problem is that RelativeLayout doesn't allow to use weight and
                plays bad with hidden childs and above/below only support one id
            -->
            <!-- Grid for active calls -->

            <com.csipsimple.ui.incall.InCallInfoGrid
                android:id="@+id/activeCallsGrid"
                android:layout_width="0dip"
                android:layout_height="match_parent"
                android:layout_weight="2"
                android:paddingBottom="4dip"
                android:paddingTop="4dip"
                tools:ignore="NestedWeights" />

            <!-- Grid for held calls -->

            <com.csipsimple.ui.incall.InCallInfoGrid
                android:id="@+id/heldCallsGrid"
                android:layout_width="0dip"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@drawable/in_call_held_background"
                android:padding="4dip"
                android:visibility="gone" />
        </LinearLayout>

        <!-- Answer slider/buttons -->

        <com.csipsimple.ui.incall.locker.InCallAnswerControls
            android:id="@+id/inCallAnswerControls"
            android:layout_width="match_parent"
            android:layout_height="0dip"
            android:layout_weight="2"
            android:visibility="gone" />

        <!-- Wraps split action bar manually managed -->

        <com.csipsimple.ui.incall.InCallControls
            android:id="@+id/inCallControls"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/incall_bottom_bar_bg" />
    </LinearLayout>

    <com.csipsimple.ui.incall.locker.ScreenLocker
        android:id="@+id/lockerOverlay"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#8000"
        android:visibility="gone" >

    </com.csipsimple.ui.incall.locker.ScreenLocker>



</RelativeLayout>

別のレイアウト ファイル in_call_card.xml があります。このコードで:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/call_card_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF"
    android:padding="2dip"
    tools:ignore="Overdraw" >

    <com.csipsimple.widgets.MaxScaleImageView
        android:id="@+id/contact_photo"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/call_action_bar"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:contentDescription="@string/empty_description"
        android:scaleType="centerCrop"
        android:src="@drawable/ic_contact_picture_180_holo_light" /> 

    <RelativeLayout
        android:id="@+id/contact_name_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#9e808080"
        android:gravity="center_vertical" >
  <!--     
            <EditText
        android:id="@+id/editText1"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:ems="10" >



        <requestFocus />
        </EditText>
       --> 

        <com.csipsimple.widgets.MarqueeTextView
            android:id="@+id/contact_name_display_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:ellipsize="marquee"
            android:marqueeRepeatLimit="marquee_forever"
            android:padding="5dip"
            android:singleLine="true"
            android:textColor="@android:color/white"
            android:textSize="25sp" />

        <com.csipsimple.widgets.MarqueeTextView
            android:id="@+id/contact_name_sip_address"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/contact_name_display_name"
            android:ellipsize="marquee"
            android:marqueeRepeatLimit="marquee_forever"
            android:padding="2dip"
            android:singleLine="true"
            android:textColor="@android:color/white"
            android:textSize="15sp" />

        <Chronometer
            android:id="@+id/elapsedTime"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:textSize="20sp" />


    </RelativeLayout>

    <TextView
        android:id="@+id/call_status_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/contact_name_bar"
        android:layout_marginTop="1dip"
        android:background="#c818617b"
        android:gravity="center_vertical|right"
        android:padding="3dip"
        android:text="@string/call_state_calling"
        android:textAllCaps="true"
        android:textColor="@android:color/white"
        android:textSize="15sp" />

    <RelativeLayout
        android:id="@+id/call_secure_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/call_status_text"
        android:layout_marginTop="1dip"
        android:background="#c8d4aa00" >


        <ImageView
            android:id="@+id/call_secure_icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_margin="2dip"
            android:contentDescription="@string/empty_description"
            android:src="@drawable/stat_sys_vp_phone_call" />

        <TextView
            android:id="@+id/call_secure_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_toRightOf="@+id/call_secure_icon"
            android:padding="2dip"
            android:textColor="@android:color/white"
            android:textSize="15sp" />
    </RelativeLayout>

    <FrameLayout
        android:id="@+id/call_action_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/end_call_bar"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignWithParentIfMissing="true"
        android:layout_marginTop="2dip"
        android:background="#000000" />

    <LinearLayout


        android:id="@+id/end_call_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_marginTop="2dip"
        android:background="@drawable/end_call_background"
        android:orientation="horizontal" >

        <!--   android:onClick="pushtotalk3" -->
         <ToggleButton
            android:id="@+id/PTT_button5"
            android:layout_width="0dp"
            android:layout_height="fill_parent"            
            android:text="@string/ptt5" 
            android:layout_weight="50"
            android:textOn="Push To Talk On"
            android:textOff="Push To Talk Off"
            android:background="@drawable/btn_lightblue_glossy"
            android:textColor="@android:color/white"
            android:textSize="15sp"
            />      

        <ImageButton
            android:id="@+id/endButton"
            style="?attr/actionButtonStyle"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="50"
            android:contentDescription="@string/done"
            android:minHeight="64dip"
            android:src="@drawable/ic_end_call" />


    </LinearLayout>


</RelativeLayout>

ContentView を他の xml ファイルに設定すると、このファイルの要素がまだ表示されますが、これがどのように機能するかわかりません。ボタンなどを見ることができるようにPTT_button5、私の問題は、このボタンにリスナーを設定したい場合、どうすればよいですか? それを参照しようとすると、Null Pointer Exception が発生します。

findViewById(R.id.PTT_button5).setOnTouchListener(this);

これは、このボタンへの参照を含まないファイルに setContentView を設定したためでしょうか。どうすればこのボタンへの参照を取得できますか? ContentView をそのファイルに設定せずにボタンが画面に表示されるため、これがどのように機能するかもわかりません。

編集:ボタンが画面上にあるので、コードからどのように行われるかを誰でも見ることができますか? https://code.google.com/p/csipsimple/source/browse/trunk/CSipSimple/src/com/csipsimple/ui/incall/?r=2170

4

2 に答える 2

2

次の基本事項を理解する必要があると思います。

1) Activity UIを配置できるウィンドウを提供しますsetContentView(View)

あなたの場合、これはView(それを呼び出すことができますcontentView)を使用して膨らませていますin_call_main.xml

2)contentViewは他のビューが含まれています。

あなたの場合、contentViewXMLレイアウトで定義されたビューが含まれています。これが、アクティビティの開始時に表示される理由です。(xml ファイルが空の場合、何も表示されません)

3からビューを追加および削除できますcontentView

これは、コードを使用して新しいビューを作成し (たとえばToggleButton)、それを に追加できることを意味しますcontentView。これToggleButtonが画面に表示されるようになりました。( の子であるためcontentView)。同様に、子ビューを削除すると、子ビューが消えます。

4) (だけでなく) 任意のビューはcontentView、レイアウト ファイルから膨張 (作成) できます。

あなたの場合、InCallCardから膨張しているビューin_call_card.xmlです。

getView()メソッドCallsAdapterが呼び出されると、(view) の新しいインスタンスInCallCardが作成されて返されます。次に にアタッチされるため、 ( contentView) 内のすべての UI 要素 (ビュー)が画面に表示されます。(大変ご迷惑をおかけしたものも含みます)InCallCardin_call_card.xmlToggleButton

したがって、ToggleButtonfromにアクセスしようonCreate()とすると null になります。にはまだ追加されてcontentViewいません。しかし、別の回答getViewで説明したように、メソッドで同じことを行うと. null ではなくなったことがわかります。リスナーをアタッチしたり、背景を変更したりできます。

于 2013-11-05T06:10:29.603 に答える
0

これは、このボタンへの参照を含まないファイルに ContentView を設定したためですか。

はい。findviewById現在のインフレートされたレイアウトで言及されている id を持つビューを探します。したがって、レイアウトのコンテンツをアクティビティに設定すると、そのレイアウトで言及されている ID を持つビューが検索されます。そうでない場合、初期化に失敗し、NullPointerException.

あなたは言う

このファイル in_call_main.xml に ContentView を設定しました

そう

  setContentView(R.layout.in_call_main);
  findViewById(R.id.PTT_button5).setOnTouchListener(this);// gives NPE
  //in_call_main.xml does not have a toogle button with id button5 

あなたin_call_card.xmlが持っている

  <ToggleButton
        android:id="@+id/PTT_button5"

しかし、もしあなたが持っているなら

  setContentView(R.layout.in_call_card);
  findViewById(R.id.PTT_button5).setOnTouchListener(this);// works
  // in_call_card.xml has a toogle button with id button5

このボタンへの参照を取得するにはどうすればよいですか?

レイアウトを膨らませないとできません。

于 2013-10-29T10:44:34.127 に答える