Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/207.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在Android中扩展两个类?_Java_Android_Class_Extends - Fatal编程技术网

Java 如何在Android中扩展两个类?

Java 如何在Android中扩展两个类?,java,android,class,extends,Java,Android,Class,Extends,我需要从一个类扩展两个类。我的类希望同时扩展ListActivity和MainActivity 我发现了一个类似的问题。 但我不知道如何实现这一点 谢谢你的帮助 必须扩展的类是ConnectionEditActivity public class ConnectionEditActivity extends ListActivity implements OnClickListener { public static Connection connectionParam; private Co

我需要从一个类扩展两个类。我的类希望同时扩展ListActivity和MainActivity

我发现了一个类似的问题。 但我不知道如何实现这一点

谢谢你的帮助

必须扩展的类是ConnectionEditActivity

public class ConnectionEditActivity extends ListActivity implements OnClickListener
{
public static Connection connectionParam;

private Connection connection;

private Button save;

private EditText name;
private EditText password;

protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    this.connection = connectionParam;

    this.save = (Button) this.findViewById(R.id.save);
    this.save.setOnClickListener(this);
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.GINGERBREAD)
    {
        // Don't need the Save button on newer devices
        android.widget.LinearLayout.LayoutParams a = (LayoutParams) this.save.getLayoutParams();
        a.height = 0;
        this.save.setLayoutParams(a);
        this.save.forceLayout();
    }

    this.name = (EditText) this.findViewById(R.id.name);
    this.password = (EditText) this.findViewById(R.id.password);

}

@Override
public boolean onCreateOptionsMenu(android.view.Menu menu)
{
    // Inflate the menu items for use in the action bar
    android.view.MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.connection_edit_menu, menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(android.view.MenuItem item)
{
    // Handle presses on the action bar items
    switch (item.getItemId())
    {
        case R.id.action_save:
            this.finish();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

protected void onResume()
{
    super.onResume();

    this.name.setText(this.connection.getName());
    this.password.setText(this.connection.getPassword());
}

protected void onPause()
{
    super.onPause();

    this.connection.setName(this.name.getText().toString());
    this.connection.setPassword(this.password.getText().toString());
    finish();
}

public void onClick(View v)
{
    if (v == this.save)
    {
        this.finish();
    }
}
}
main活动

    public abstract class MainActivity extends ActionBarActivity
    {
    protected ListView mDrawerList;
    protected DrawerLayout mDrawer;
    private CustomActionBarDrawerToggle mDrawerToggle;
    private String[] menuItems;
    String LOG_TAG = "Remote It";

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(WindowCompat.FEATURE_ACTION_BAR);
        // getSupportActionBar().hide();
        setContentView(R.layout.activity_main_drawer);

        // enable ActionBar app icon to behave as action to toggle nav drawer
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);

        mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);

        // set a custom shadow that overlays the main content when the drawer
        // opens
        mDrawer.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);

        _initMenu();
        mDrawerToggle = new CustomActionBarDrawerToggle(this, mDrawer);
        mDrawer.setDrawerListener(mDrawerToggle);
    }
private void _initMenu()
    {
        NsMenuAdapter mAdapter = new NsMenuAdapter(this);

        // Add Header
        mAdapter.addHeader(R.string.ns_menu_main_header);

        // Add first block

        menuItems = getResources().getStringArray(R.array.ns_menu_items);
        String[] menuItemsIcon = getResources().getStringArray(R.array.ns_menu_items_icon);

        int res = 0;
        for (String item : menuItems)
        {

            int id_title = getResources().getIdentifier(item, "string", this.getPackageName());
            int id_icon = getResources().getIdentifier(menuItemsIcon[res], "drawable", this.getPackageName());

            NsMenuItemModel mItem = new NsMenuItemModel(id_title, id_icon);
            // if (res==1) mItem.counter=12; //it is just an example...
            // if (res==3) mItem.counter=3; //it is just an example...
            mAdapter.addItem(mItem);
            res++;
        }
        // Second Block
        mAdapter.addHeader(R.string.ns_menu_main_header2);

        menuItems = getResources().getStringArray(R.array.ns_menu_itemss);
        String[] menuItemsIcons = getResources().getStringArray(R.array.ns_menu_items_iconss);

        int ress = 0;
        for (String item : menuItems)
        {

            int id_title = getResources().getIdentifier(item, "string", this.getPackageName());
            int id_icon = getResources().getIdentifier(menuItemsIcons[ress], "drawable", this.getPackageName());

            NsMenuItemModel mItem = new NsMenuItemModel(id_title, id_icon);
            // if (res==1) mItem.counter=12; //it is just an example...
            // if (res==3) mItem.counter=3; //it is just an example...
            mAdapter.addItem(mItem);
            res++;
        }
        mDrawerList = (ListView) findViewById(R.id.drawer);
        if (mDrawerList != null)
            mDrawerList.setAdapter(mAdapter);

        mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState)
    {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
        mDrawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig)
    {
        super.onConfigurationChanged(newConfig);
        mDrawerToggle.onConfigurationChanged(newConfig);
    }
@Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        /*
         * The action bar home/up should open or close the drawer.
         * ActionBarDrawerToggle will take care of this.
         */
        if (mDrawerToggle.onOptionsItemSelected(item))
        {
            return true;
        }

        // Handle your other action bar items...
        return super.onOptionsItemSelected(item);
    }

    private class CustomActionBarDrawerToggle extends ActionBarDrawerToggle
    {

        public CustomActionBarDrawerToggle(Activity mActivity, DrawerLayout mDrawerLayout)
        {
            super(mActivity, mDrawerLayout, R.drawable.ic_drawer, R.string.ns_menu_open, R.string.ns_menu_close);
        }

        @Override
        public void onDrawerClosed(View view)
        {
            getSupportActionBar().setTitle(getString(R.string.ns_menu_close));
            supportInvalidateOptionsMenu(); // creates call to
                                            // onPrepareOptionsMenu()
        }

        @Override
        public void onDrawerOpened(View drawerView)
        {
            getSupportActionBar().setTitle(getString(R.string.ns_menu_open));
            supportInvalidateOptionsMenu(); // creates call to
                                            // onPrepareOptionsMenu()
        }
    }

    private class DrawerItemClickListener implements ListView.OnItemClickListener
    {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id)
        {
            mDrawer.closeDrawer(mDrawerList);
            switch (position)
            {
                case 1:
                    Intent a = new Intent(MainActivity.this, Home.class);
                    startActivity(a);
                    break;
                case 2:
                    Intent ac = new Intent(MainActivity.this, ConnectionListActivity.class);
                    startActivity(ac);
                    break;
                                default:
            }

        }
公共抽象类MainActivity扩展了ActionBarActivity
{
受保护的ListView mDrawerList;
受保护抽屉布置图;
私有CustomActionBarDrawerToggle mDrawerToggle;
私有字符串[]菜单项;
字符串LOG_TAG=“远程It”;
@凌驾
创建时受保护的void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
supportRequestWindowFeature(WindowCompat.FEATURE\u ACTION\u BAR);
//getSupportActionBar().hide();
setContentView(R.layout.activity\u main\u drawer);
//启用ActionBar应用程序图标作为切换导航抽屉的操作
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
mDrawer=(抽屉布局)findViewById(R.id.抽屉布局);
//当抽屉打开时,设置覆盖主要内容的自定义阴影
//打开
mDrawer.setDrawerShadow(R.drawable.drawer\u shadow,GravityCompat.START);
_initMenu();
mDrawerToggle=新的CustomActionBarDrawerToggle(这个,mDrawer);
mDrawer.setDrawerListener(mDrawerToggle);
}
私有void _initMenu()
{
NsMenuAdapter mAdapter=新的NsMenuAdapter(此);
//添加标题
mAdapter.addHeader(R.string.ns\u菜单\u主\u标题);
//添加第一个块
menuItems=getResources().getStringArray(R.array.ns\u菜单项);
String[]menuItemsIcon=getResources().getStringArray(R.array.ns\u菜单项\u图标);
int res=0;
用于(字符串项:菜单项)
{
int id_title=getResources().getIdentifier(项,“字符串”,this.getPackageName());
int id_icon=getResources().getIdentifier(menuItemsIcon[res],“drawable”,this.getPackageName());
NSMENUITEMODEL mItem=新的NSMENUITEMODEL(id\u标题,id\u图标);
//如果(res==1)mItem.counter=12;//这只是一个例子。。。
//如果(res==3)mItem.counter=3;//这只是一个例子。。。
补充材料(mItem);
res++;
}
//第二街区
mAdapter.addHeader(R.string.ns\u menu\u main\u header2);
menuItems=getResources().getStringArray(R.array.ns\u菜单项);
String[]menuItemsIcons=getResources().getStringArray(R.array.ns\u菜单项\u图标);
int ress=0;
用于(字符串项:菜单项)
{
int id_title=getResources().getIdentifier(项,“字符串”,this.getPackageName());
int id_icon=getResources().getIdentifier(menuItemsIcons[ress],“drawable”,this.getPackageName());
NSMENUITEMODEL mItem=新的NSMENUITEMODEL(id\u标题,id\u图标);
//如果(res==1)mItem.counter=12;//这只是一个例子。。。
//如果(res==3)mItem.counter=3;//这只是一个例子。。。
补充材料(mItem);
res++;
}
mDrawerList=(ListView)findViewById(R.id.drawer);
if(mDrawerList!=null)
mDrawerList.setAdapter(mAdapter);
setOnItemClickListener(新的DrawerItemClickListener());
}
@凌驾
后期创建时受保护的空(捆绑包savedInstanceState)
{
super.onPostCreate(savedInstanceState);
//在onRestoreInstanceState发生后同步切换状态。
mDrawerToggle.syncState();
}
@凌驾
公共无效OnConfiguration已更改(配置newConfig)
{
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项)
{
/*
*home/up操作栏应打开或关闭抽屉。
*ActionBarDrawerToggle会处理好的。
*/
如果(MDRAWERTOGLE.onOptionsItemSelected(项目))
{
返回true;
}
//处理其他操作栏项。。。
返回super.onOptionsItemSelected(项目);
}
私有类CustomActionBarDrawerToggle扩展了ActionBarDrawerToggle
{
公共CustomActionBarDrawerToggle(活动权限、抽屉布局MDDrawerLayout)
{
超级(mActivity、mDrawerLayout、R.drawable.ic\u抽屉、R.string.ns\u菜单打开、R.string.ns\u菜单关闭);
}
@凌驾
公共无效onDrawerClosed(视图)
{
getSupportActionBar().setTitle(getString(R.string.ns_菜单_关闭));
SupportInvalidateOptions菜单();//创建对的调用
//onPrepareOptions菜单()
}
@凌驾
打开图纸上的公共空白(视图抽屉视图)
{
getSupportActionBar().setTitle(getString(R.string.ns_菜单_打开));
SupportInvalidateOptions菜单();//创建对的调用
//onPrepareOptions菜单()
}
}
私有类DrawerItemClickListener实现ListView.OnItemClickListener
{
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id)
{
mDrawer.closeDrawer(mDrawerList);
开关(位置)
{
案例1:
意向a=新意向(MainActivity.this,Home.class);
星触觉(a);
打破
案例2:
Intent ac=新Intent(MainActivity.this、ConnectionListActivity.class);
星触觉(ac);
打破
违约:
}
}
编辑

我需要
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
 ...
}
public class NewClass extends OldClass1 {

   private OldClass2 mOldClass2 = new OldClass2();

   @Override
   public methodFromOldClass1() {
   }

   public methodFromOldClass2() {
       mOldClass2.methodFromOldClass2(); 
   }

}