【问题标题】:In Android SDK/Java, how do I manipulate XML Views(imagebuttons, textviews) from a method in a fragment?在 Android SDK/Java 中,如何从片段中的方法操作 XML 视图(图像按钮、文本视图)?
【发布时间】:2015-03-28 03:45:43
【问题描述】:

解决了!感谢大家的帮助。

我在将片段作为一个概念而苦苦挣扎,尤其是似乎停留在这一件事上。我想要做的是,使用我的片段,操纵它自己的布局数据。每次我尝试从 Fragment 中访问 ImageButton 时,它都会使应用程序崩溃。从活动中可以正常工作。我只是从根本上误解了 Fragments 吗?

代码(缩小尺寸)-

这是调用我的片段的活动的开始:

Display.java

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

        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

        MyFragmentClass MyFragment = new MyFragmentClass();

        fragmentTransaction.add(R.id.fragment_container, MyFragment);
        fragmentTransaction.commit();

该活动的 XML:

activity_display.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingRight="@dimen/activity_horizontal_margin"
                android:paddingTop="@dimen/activity_vertical_margin"
                android:paddingBottom="@dimen/activity_vertical_margin"
                tools:context="com.mycompanyname.myprojectname.Display"
                android:id="@+id/display_layout">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/fragment_container">
    </FrameLayout>

</RelativeLayout>

片段

MyFragmentClass.java
public class MyFragmentClass extends Fragment
{
 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        return inflater.inflate(R.layout.fragment_layout_screen, container, false);;

    }

    public void MethodTest()
    {

    }

}

片段的 XML 文件:

fragment_layout_screen.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/fragment_layout_screen"
    tools:context="com.mycompanyname.myprojectname.MyFragmentClass">

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/my_button"
        android:clickable="true"
        android:onClick="buttonPress"
        android:src="@drawable/button"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"/>

</RelativeLayout>

基本上,我最初在 Display.java 中拥有所有这些,但想添加片段,所以我试图将内容移出并移入片段,但我仍然需要操作 xml 信息的能力,我只是可以'吨。

从 Display.java 活动内部,我可以像这样轻松调用 ImageButton:

ImageButton myButton = (ImageButton) findViewById(R.id.my_button)

但如果我从片段中的 MethodTest 执行相同操作,则应用程序崩溃。

我在这里搜索了很多建议,尝试了各种解决方案:findViewById in Fragment,但这些似乎都不起作用。

我一直在阅读此设置:http://developer.android.com/guide/components/fragments.html,但似乎找不到我做错了什么。

我们将不胜感激,如果您对我的设置有任何疑问,请提出。

【问题讨论】:

  • getView().findViewById(R.id.my_button) 在 Fragment 中工作吗?
  • 这会在从 MethodTest() 调用应用程序时立即崩溃

标签: java android xml android-fragments


【解决方案1】:

声明片段private View rootView;onCreateView(...) 上设置rootView = inflater.Inflate(..);,然后以ImageButton mButton =(ImageButton) rootView.findViewById(..); 访问图像按钮

【讨论】:

  • 我试过了,如果我从 OnCreateView 之外的任何地方调用 methodTest(),它就会崩溃。这是它工作的唯一方法吗?我不能在它自己的 OnCreateView 之外调用片段方法吗?
  • 能否提供片段代码?问题很可能是在 OnCreateView 之前调用了该方法,而您在 methodTest() 中使用了一些需要布局准备好的内容。
  • public class MyFragmentClass extends Fragment { private View view; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { view = inflater.inflate(R.layout.fragment_layout_screen, container, false); return view; } public void MethodTest() { ImageButton mButton = (ImageButton) view.findViewById(mybutton); } }
  • 刚刚编辑了我实际操作的更新。那只是一个普通版本,回车太快了
  • 从片段调用MethodTest() 的位置尚未初始化其布局。它要么是 view.findViewById(mybutton);,要么返回 null。那不应该是view.findViewById(R.id.mybutton);吗?
【解决方案2】:
Do like this in fragment

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.gridview, container, false);


        GridView gridview = (GridView) view.findViewById(R.id.grid);

return view ;

【讨论】:

  • 所以我想我不完全理解您建议在哪里进行更改。那么您是否建议我将布局从 RelativeLayout 更改为 GridView?或者 GridView 是否被用作我可能拥有的任何布局的支架?如果我将 GridView 与我的相对布局交换,我仍然无法从 MethodTest() 调用它。如果我在 onCreateView 之外创建它,我可以从 MethodTest() 调用它,但它仍然每次都崩溃。我让它工作的唯一方法是,如果我在片段之外创建它,然后从 within OnCreateView 调用 MethodTest()。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-23
相关资源
最近更新 更多