简体   繁体   English

扩展类中的方法不起作用

[英]Methods in extended class are not working

I created a menu, where you can swipe up and down. 我创建了一个菜单,您可以在其中上下滑动。 I created this menu as another activity . 我将此菜单创建为另一个activity Now, I need this menu to be added to on other activities . 现在,我需要将此菜单添加到其他activities

Following is the code of my menu(SlidingDrawer) 以下是我的菜单代码(SlidingDrawer)

Java Java的

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class DropDownMenu extends Activity {

    private TextView addInquiry, addEvent, additionalInfo, addToContacts;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_drop_down_menu);

        //Intializing instance variables
        addInquiry = (TextView)findViewById(R.id.menu_add_inquiry);
        addEvent = (TextView)findViewById(R.id.menu_add_event);
        additionalInfo = (TextView)findViewById(R.id.menu_additional_info);
        addToContacts = (TextView)findViewById(R.id.menu_add_to_contacts);

        //Register the Listeners
        addInquiry.setOnClickListener(new AddInquiryEvent());


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.drop_down_menu, menu);
        return true;
    }

    //Test Button
    private class AddInquiryEvent implements OnClickListener
    {

        @Override
        public void onClick(View arg0) 
        {
            // TODO Auto-generated method stub
            Intent intent = new Intent(DropDownMenu.this,NewLead.class);
            startActivity(intent);

        }

    }

}

XML XML格式

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <SlidingDrawer
        android:id="@+id/SlidingDrawer"
        android:layout_width="wrap_content"
        android:layout_height="250dip"
        android:layout_alignParentBottom="true"
        android:content="@+id/contentLayout"
        android:handle="@+id/slideButton"
        android:orientation="vertical" >

        <Button
            android:id="@+id/slideButton"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@android:drawable/bottom_bar" >
        </Button>

        <LinearLayout
            android:id="@+id/contentLayout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="10dip"
            android:background="#cbcbcc" >

            <TextView
                android:id="@+id/menu_add_inquiry"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:padding="5dp"
                android:text="@string/add_inquiry" 
                android:textColor="#ffffff"
                android:clickable="true"
                android:textAppearance="?android:attr/textAppearanceMedium" />




        </LinearLayout>
    </SlidingDrawer>

</RelativeLayout>

Following is the Code of the other activity 以下是其他activity的代码

XML XML格式

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/sales_inqury_main_layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:clickable="true"
     android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".SalesInqury" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerInParent="true"
        android:text="@string/sales_inqury"
        android:textSize="40sp" />


        <include layout = "@layout/activity_drop_down_menu"
            android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"/>


</RelativeLayout>

Java Java的

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class SalesInqury extends DropDownMenu {

    private ImageView addNewSalesInqury;
    private RelativeLayout salesInquryMainLayout;
    private TextView testEditSales;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);     
        setContentView(R.layout.activity_sales_inqury);

        //Intializing instance variables
        addNewSalesInqury = (ImageView)findViewById(R.id.add_new_sales_inqury_btn);
        salesInquryMainLayout = (RelativeLayout)findViewById(R.id.sales_inqury_main_layout);
        testEditSales = (TextView)findViewById(R.id.testWord);



    }
}

But, there is an issue. 但是,有一个问题。 Even thougn I can open the menu in this other activity, I can't click on it's Buttons (TextViews) and navigate to other activities. 即使您可以在其他活动中打开菜单,也无法单击按钮(TextViews)并导航至其他活动。 Why is this? 为什么是这样?

I think it's because after you've set a contentView in your DropDownMenu activity and bound the listener to the button, in SalesInquiry you set a different contentView. 我认为这是因为在DropDownMenu活动中设置了contentView并将侦听器绑定到按钮之后,在SalesInquiry设置了另一个contentView。 Although this includes the menu-layout, the Views are created anew, so there's no Listener bound to the button anymore. 尽管这包括菜单布局,但视图是重新创建的,因此不再有绑定到该按钮的侦听器。

A solution would either be to specify the menu callbacks in the layout xml via android:onClick="..." or by having a separate method in DropDownMenu that adds the Listener and that you call from your SalesInquiry class after setting the content view. 一种解决方案是通过android:onClick="..."在布局xml中指定菜单回调,或者通过在DropDownMenu中使用单独的方法来添加侦听器,并设置内容视图SalesInquiry类调用它。

Example 1, specifying callback in XML 示例1,在XML中指定回调

Add android:onClick="onMenuItemClicked" to menu item TextView android:onClick="onMenuItemClicked"到菜单项TextView

<TextView
        android:id="@+id/menu_add_inquiry"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:text="@string/add_inquiry" 
        android:textColor="#ffffff"
        android:clickable="true"
        android:onClick="onMenuItemClicked"
        android:textAppearance="?android:attr/textAppearanceMedium"/>

Add this method to DropDownMenu class and remove the AddInquiryEvent class and the registering of the listeners. 将此方法添加到DropDownMenu类,并删除AddInquiryEvent类和侦听器的注册。

public void onMenuItemClicked(View view) {
    switch (view.getId()) {
    case R.id.menu_add_inquiry:
        Intent intent = new Intent(DropDownMenu.this,NewLead.class);
        startActivity(intent);
        break;
    case R.id.menu_other:
        // handle other menu item
        break;
    // ... and so on ...
    default:;
    }
}

Example 2, bind listeners in a separate method 示例2,以单独的方法绑定侦听器

In DropDownMenu move the registering of the Listeners to a separate method: DropDownMenu ,将侦听器的注册移动到单独的方法:

protected void registerListeners() {
    //Register the Listeners
    findViewById(R.id.menu_add_inquiry).setOnClickListener(new AddInquiryEvent());
}

Note, that you have to find the view again, as the one kept in the local variable is the wrong one, after SalesInquiry set it's content view. 请注意,您必须再次查找该视图,因为在SalesInquiry设置其内容视图后,保留在局部变量中的视图是错误的视图。

Call the method in SalesInquiry after setting the content view. 设置内容视图后,在SalesInquiry中调用该方法。

  setContentView(R.layout.activity_sales_inqury);
  registerListeners();

The problem is that you are attaching the listener to addInquiry in the DropDownMenu : 问题是您将侦听器附加到DropDownMenu addInquiry

//Register the Listeners
addInquiry.setOnClickListener(new AddInquiryEvent());

This listener is not attached to the TextView in the extended class, since the setContentView is called and layout is refreshed. 该监听器未附加到扩展类的TextView中,因为调用了setContentView并且刷新了布局。

Possible solution, if you are not using the DropDownMenu activity on its own then simply (remove setContentView): 可能的解决方案,如果您不DropDownMenu使用DropDownMenu活动,则只需简单地(删除setContentView):

@Override 
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); //in DropDownMenu
    // setContentView(R.layout.activity_drop_down_menu);
...
...

Not the best solution but would work. 不是最好的解决方案,但可以。

Update: Better Solution (1) Let the subclass decide which layout to show. 更新:更好的解决方案 (1)让子类决定要显示的布局。 This layout must contain the menu (which is true in your case). 此布局必须包含菜单(在您的情况下为true)。

public class DropDownMenu extends Activity {

    private TextView addInquiry, addEvent, additionalInfo, addToContacts;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        _setContentView();

        //Intializing instance variables
        addInquiry = (TextView)findViewById(R.id.menu_add_inquiry);
        addEvent = (TextView)findViewById(R.id.menu_add_event);
        additionalInfo = (TextView)findViewById(R.id.menu_additional_info);
        addToContacts = (TextView)findViewById(R.id.menu_add_to_contacts);

        //Register the Listeners
        addInquiry.setOnClickListener(new AddInquiryEvent());
    }

    protected void _setContentView() {
        setContentView(R.layout.activity_drop_down_menu);
    }
    ...
    ...
}

@override the _setContentView method so that we do not set the view twice. @override _setContentView方法,这样我们就不会两次设置视图。

public class SalesInqury extends DropDownMenu {

    ...
    ...

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);     

        //Intializing instance variables
        addNewSalesInqury = (ImageView)findViewById(R.id.add_new_sales_inqury_btn);
        salesInquryMainLayout = (RelativeLayout)findViewById(R.id.sales_inqury_main_layout);
        testEditSales = (TextView)findViewById(R.id.testWord);
    }

    @Override
    protected void _setContentView() {
        // need to make sure this layout contains the menu (otherwise NullPointerException may arise)
        setContentView(R.layout.activity_sales_inqury); 
    }
}

I'm not sure. 我不确定。 but can you please try with extending the DropDownMenu-Activity instead of Activity. 但您能否尝试扩展DropDownMenu-Activity而不是Activity。

 public class NewActivity extends DropDownMenu {
 // override which you want to override 
}

And make sure DropDownMenu in such a way that you can extends what you want to implements. 并确保DropDownMenu可以扩展您要实现的内容。 Hope you have got my point. 希望你明白我的意思。

Consistency Issue (Recommendation) 一致性问题(推荐)

There is a HUGE misnomer in your class name, you should NOT call an Activity DropDownMenu. 有一个在你的类名一个巨大的用词不当, 应该叫活动DropDownMenu。 This is confusing, you should instead call it DropDownActivity or something like that. 这很令人困惑,您应该改名为DropDownActivity或类似的名称。 Also do you really need to use an Activity? 您还真的需要使用活动吗? You do realize a View can still listen to the click of a button right? 您确实知道View仍然可以听按钮的点击吗? Not only an Activity can listen to button clicks. 不仅活动可以听按钮的点击。 If you encapsulate all of this in a View and have a dedicated listener for that view, then you wouldn't have any need for another Activity. 如果将所有这些封装在一个视图中,并具有该视图的专用侦听器,那么您就不需要其他活动。

Possible Solution 可能的解决方案

It's likely related to the activity/component you have registered to listen to the buttons is not correct. 您注册的监听按钮的活动/组件可能不正确。 Validate your code so that you can at least get the correct response of the onClick. 验证您的代码,以便至少能够获得onClick的正确响应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM