如何访问 TabActivity 中活动的视图?

发布于 2024-12-17 23:01:43 字数 2118 浏览 2 评论 0原文

我有一个 TabActivity ,其中包含旨在拆分表单的其他活动。 TabActivity 在其布局中有一个按钮,旨在从 TabActivity 中包含的所有活动的所有表单相关视图收集数据并存储它。我遇到的问题是 TabActivity 似乎无法访问这些视图;当我使用其中之一调用 findViewById() 时,我得到一个 NullPointerException

文档似乎很少介绍 TabActivity 究竟如何控制它所包含的活动。如果它在从一项活动切换到另一项活动时破坏了一项活动,那么我所处的情况就有意义了。我想知道实现上述目标的最佳方法。

src/com/vendor/MyTabActivity.java:

public class MyTabActivity extends TabActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_tab_activity);
        final Button saveButton = (Button) findViewById(R.id.save_button);
        saveButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // NullPointerException happens here
                String fieldValue = ((TextView) findViewById(R.id.text_field)).getText().toString();
            }
        });
    }
}

res/layout/my_tab_activity.xml:

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost">
    <LinearLayout>
        <TabWidget android:id="@android:id/tabs"/>
        <FrameLayout android:id="@android:id/tabcontent" />
        <Button android:id="@+id/save_button"/>
    </LinearLayout>
</TabHost>

src/com/vendor/NestedActivity.java:

public class NestedActivity extends Activity {  
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.nested_activity);
    }
}

res/layout/nested_activity.xml:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android">  
    <RelativeLayout>
        <EditText android:id="@+id/text_field"/>
    </RelativeLayout>
</ScrollView>

I've got a TabActivity containing other activities intended to split up a form. The TabActivity has in its layout a button intended to collect the data from all the form-related views across all the activities contained within the TabActivity and store it. The problem I'm running into is that the TabActivity doesn't appear to have access to these views; when I call findViewById() with one of them, I get a NullPointerException.

The documentation seems sparse about exactly how TabActivity works with respect to controlling the activities it contains. If it destroys an activity when switching from it to a different one, the situation I'm in would make sense. I'd like to know the best approach for accomplishing the goal described above.

src/com/vendor/MyTabActivity.java:

public class MyTabActivity extends TabActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_tab_activity);
        final Button saveButton = (Button) findViewById(R.id.save_button);
        saveButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // NullPointerException happens here
                String fieldValue = ((TextView) findViewById(R.id.text_field)).getText().toString();
            }
        });
    }
}

res/layout/my_tab_activity.xml:

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost">
    <LinearLayout>
        <TabWidget android:id="@android:id/tabs"/>
        <FrameLayout android:id="@android:id/tabcontent" />
        <Button android:id="@+id/save_button"/>
    </LinearLayout>
</TabHost>

src/com/vendor/NestedActivity.java:

public class NestedActivity extends Activity {  
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.nested_activity);
    }
}

res/layout/nested_activity.xml:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android">  
    <RelativeLayout>
        <EditText android:id="@+id/text_field"/>
    </RelativeLayout>
</ScrollView>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

美煞众生 2024-12-24 23:01:43

您的问题来自 MyTabActivity 中的这两行...

setContentView(R.layout.my_tab_activity);

...并且...

String fieldValue = ((TextView) findViewById(R.id.text_field)).getText().toString();

...尽管您显然知道 findViewById(R.id.text_field) 是是什么原因造成的。

使用 findViewById(...) 仅在尝试访问已作为当前 Activity 的一部分扩展的 UI 元素时才有效。由于 my_tab_activity.xml 中不存在资源 ID 为 R.id.text_fieldTextView,因此它永远无法工作。

TabHost / TabActivity 访问作为选项卡内容的活动是很棘手的。我的建议是使用 SharedPreferences ,它可以从应用程序中的任何位置访问。一旦 TextView(或任何其他用户输入项)发生更改,请使用标识其来自哪个活动/选项卡的“键”将其保存到 SharedPreferences。从此,TabActivity 就可以轻松整理数据了。

Your problem comes from these two lines in MyTabActivity...

setContentView(R.layout.my_tab_activity);

...and...

String fieldValue = ((TextView) findViewById(R.id.text_field)).getText().toString();

...although you obviously know the findViewById(R.id.text_field) is what's causing it.

Using findViewById(...) only works when trying to access UI elements which have been inflated as part of your current Activity. As there isn't a TextView with the resource id of R.id.text_field in the my_tab_activity.xml, it's never going to work.

Accessing activities which are tab content from the TabHost / TabActivity is tricky. My suggestion would be to use SharedPreferences which can be accessed from everywhere in your app. Once a TextView (or any other user-input item) is changed, save it to a SharedPreferences using a 'key' which identifies which activity/tab it came from. From then on, the TabActivity can collate the data easily.

只是一片海 2024-12-24 23:01:43

您可以使用 getLocalActivityManager()getCurrentActivity()。对于您返回的活动对象,您可以执行 Activity.findViewById() 来获取对特定活动内部视图的引用。但要指出 TabActivity 已被弃用,您应该使用 Fragments 来做你正在寻找的事情。如果您的目标版本低于 3.0,则可以使用兼容性库访问片段。

You can get a reference to activities running inside of the tab activity using getLocalActivityManager() or getCurrentActivity(). For the activity object you get back you can do activity.findViewById() to get a reference to a view inside of the specific activity. But to point out TabActivity has been deprecated and you should be using Fragments to do what you are looking for. If you are targeting a version of Android earlier than 3.0 you can use the compatibility library to access fragments.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文