【问题标题】:How to call inner class from outer class using intent in android?如何在android中使用intent从外部类调用内部类?
【发布时间】:2013-07-29 07:05:25
【问题描述】:

我正在开发 android 应用程序和应用程序,其操作栏包含刷新按钮和溢出菜单,操作栏下方有两个堆叠选项卡。当我单击刷新按钮时,将为该任务刷新单个选项卡,我想使用意图从外部类调用内部类,以便我可以从服务器刷新内部类。

当我尝试从外部调用内部类时使用意图的类我遇到了 ActivityNotfound 异常之类的异常,但我已经在解决后的清单中提到了内部类。我尝试了很多方法,但没有奏效。

下面是我的类结构

public class Fragment extends SherlockFragmentActivity {
    ActionBar actionBar = getSupportActionBar();

    actionBar.setDisplayHomeAsUpEnabled(false);

    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    Tab tab = actionBar.newTab().
            setText("Tab1").
            setTabListener(new Tab1())
            ;
    actionBar.addTab(tab);

    tab = actionBar.newTab().
            setText("Tab2").
            setTabListener(new Tab2())
            ;
    actionBar.addTab(tab);

    //I am tried number ways to refesh the particlar tab but not achieve my goal
    public void Refresh() {

        /* I am also write Refresh method on individual tab and call from 
        onOptionsItemSelected but not working*/

        Intent href = new Intent(Fragment.this,
                Tab1.class);  
        startActivity(href);
        finish();
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
        case R.id.menu_refresh:
            Refresh();
            return true;
        default:
            break;
        }
    }
    //inner class for tab first 
    public class Tab1 extends SherlockListFragment implements ActionBar.TabListener{

        // here implementating the TabListener method

        //here I am loading the list from server
        private class LoadProjects extends AsyncTask<String, String, String> {
            //call webservice
        }
    } 

    // inner class for tab second
    public class Tab2 extends SherlockListFragment implements ActionBar.TabListener{

        // here implementating the TabListener method

        //here I am loading the list from server

        private class LoadProjects extends AsyncTask<String, String, String> {
            //call webservice
        }
    } 
}

androidmanifest.xml

<application>
        <activity
            android:name="com.ojaswitech.bookingscape.Fragment"
            android:configChanges="orientation|keyboardHidden"
            android:label=""
            android:logo="@drawable/action_bar_logo"
            android:theme="@style/Theme.New_theme_bs" >
        </activity>
        <activity
            android:name="com.ojaswitech.bookingscape.Fragment$Tab1"
            android:configChanges="orientation|keyboardHidden" >
        </activity>
        <activity
            android:name="com.ojaswitech.bookingscape.Fragment$Tab2"
            android:configChanges="orientation|keyboardHidden" />
    </application>

请任何人帮助我如何完成上述任务。 提前致谢

【问题讨论】:

  • Tab1 类不是 Activity 而是 Fragment。 startActivity 方法仅适用于活动。片段在 FragmentManager 的帮助下显示。
  • @Herrmann 你有什么解决办法吗?

标签: android android-intent


【解决方案1】:

您应该将 Tab1 设为新文件中的公共类,而不是内部类。这将解决 Refresh() 方法“找不到活动”异常。

【讨论】:

  • 我已经做过类似的工作,如果您需要示例代码,请告诉我。
  • 我已经在这里发布了我的完整代码。请检查一下。stackoverflow.com/questions/17918198/…希望对您有所帮助。
【解决方案2】:

我能问一下你为什么要在这里使用Intents吗?我的意思是意图是用于启动或与活动通信的抽象/包装器。请阅读Fragment 概念,以更好地了解片段如何与其宿主活动通信,反之亦然。

在这种情况下,您可能需要在子片段中实现 updateUI 或 refreshUI 方法。单击刷新按钮时,获取您的片段(通过 id 或实例)并调用孩子的更新/刷新方法。

查看How to refresh fragment tab content on button click的解决方案

编码愉快!

【讨论】:

  • 我从第一个链接不明白我应该在刷新方法()中写什么。
  • 哦!对不起。第一个链接是让您开始了解片段,以及您应该了解的基本内容。只是出于好奇,您是否正在尝试使用 Fragment,例如这是您第一次在应用程序中使用 Fragment 吗?根据您的回答,我或许可以为您指明安全的地方。
  • 是的,你是对的,这是我第一次使用片段。谢谢
  • 哦!请阅读我添加的片段介绍(第一个链接)。作为开发人员,您应该意识到这一点。总结:就像 Herrmann 建议的 Fragments 需要一个主机 Activity 才能存在。将 Fragment 视为 Activity 的一部分/部分,它有自己的生命周期,接收自己的输入事件,您可以在 Activity 运行时添加或删除它。
【解决方案3】:

你可以在your_activity_context.recreate()的帮助下刷新你的片段

【讨论】:

  • 对我来说工作得很好..!!谢谢安德兰。
猜你喜欢
  • 1970-01-01
  • 2013-04-26
  • 1970-01-01
  • 1970-01-01
  • 2011-02-18
  • 2023-03-23
  • 2017-08-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多