无法从我的Button ImageView接收点击事件,让HomeScreen扩展MyActivity

时间:2013-02-06 12:59:38

标签: android layout onclicklistener

我没有从我的按钮接收点击事件。我想知道为什么。

我有一个LinearLayout,操作栏的顶部有一个include布局,下面我有一个按钮。我无法接收

的活动
  1. 操作栏上的“mylocation图标”,位于
  2. 下方
  3. 下一步按钮
  4. 放大/缩小按钮
  5. 我在这里缺少什么?

    enter image description here enter image description here

    主要布局location.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    
        <include
            android:id="@+id/actionbar"
            android:layout_alignParentTop="true"
            layout="@layout/actionbar" />
    
        <Button
            android:id="@+id/next"
            <!-- removed unnecesary attr's -->        
            android:background="#f6461d"
            android:clickable="true"
            android:enabled="true"
            android:text="Next"/>
    
    </RelativeLayout>
    

    操作栏

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_alignParentTop="true"
        android:background="#EEeeeee4"
        <!-- removed unnecesary attr's --> >
    
        <LinearLayout
            <!-- removed unnecesary attr's --> 
            android:orientation="horizontal" >
    
            <Button
                android:id="@+id/icruiseon"
                <!-- removed unnecesary attr's --> 
                android:background="@drawable/curbcablogo" />
    
            <Button
                android:id="@+id/refresh"
                <!-- removed unnecesary attr's --> 
                android:background="@drawable/refresh" />
    
            <ImageView
                android:id="@+id/connectStatus"
                <!-- removed unnecesary attr's --> 
                android:src="@drawable/disconnect" />
    
            <ImageView
                android:id="@+id/busy"
                <!-- removed unnecesary attr's --> 
                android:clickable="false"
                android:src="@drawable/busy" />
    
            <TextView
                android:id="@+id/subscriberInbox"
                <!-- removed unnecesary attr's --> 
                android:background="@drawable/incomingprovider" />
        </LinearLayout>
    
        <LinearLayout
                <!-- removed unnecesary attr's -->  >
    
            <TextView
                android:id="@+id/currentAddress"
                <!-- removed unnecesary attr's --> 
                android:text="Receiving address....." />
    
            <ImageView
                android:id="@+id/whereAmI"
                <!-- removed unnecesary attr's --> 
                android:src="@drawable/mylocation" />
        </LinearLayout>
    
    </LinearLayout>
    

    我的活动文件

    public class MyActivity {
    onCreate() {
        setContentView(R.layout.location) ;
    
            next = (Button) this.findViewById(R.id.next);
            next.setOnClickListener(this);
    
            whereAmI = (ImageView) this.findViewById(R.id.whereAmI);
            whereAmI.setOnClickListener(this) ;
    
    @Override
        public void onClick(View v) {
            if (v == next) {
                Toast.makeText(this, "searching", Toast.LENGTH_LONG).show() ;
                new Search(this, source, destination).execute(null);
            } else if (v == whereAmI) {
                Toast.makeText(this, "centering", Toast.LENGTH_LONG).show() ;
                location.getController().setCenter(source);
                new GetGeoAddress(this, source).execute(null);
            }
        }
    

    AnotherActivity

    public class HomeActivity extends MyActivity {
    onCreate() {
          //No setContentView here ; I expect that the super class MyActivity will setContentView.
    

    编辑:从布局和代码中删除MapView,以消除和调试。我仍然没有得到点击事件。我在这里缺少什么?

    编辑2:上面添加了更多更改,我怀疑这与Activity Inheritance有关。我知道我错过了一些基本的东西。它是什么?

1 个答案:

答案 0 :(得分:1)

修正了问题。活动之间的继承会混淆onClickListner。

<强>父类

public class SuperClass extends Activity implements OnClickListner {
     public onCreate() {
         setContentView(R.layout.layoutwithView_ABC) ;
         ABC.setOnClickListener(this) ;
     ...
     public void onClick() {
         Toast ("this wont show up") ;
     }
     ...
}

SubClass,作为清单文件的第一个活动

public class SubClass extends SuperClass implements OnClickListner {
     public onCreate() {
         //NOT init here, setContentView(R.layout.layoutwithView_ABC) ;
     ...
     public void onClick() {
         Toast ("will show up") ;
     }
     ...
}

希望我已经正确地代表了答案。即使我在超类中使用了setOnClickListerer for ABC,也会调用onClick of SubClass,因为它的“覆盖”,简单答案。