【问题标题】:how to edit textview in fragment from activity如何从活动中编辑片段中的文本视图
【发布时间】:2014-08-24 17:06:27
【问题描述】:

已解决

我想将新文本设置为TextView。我在MainActivity 中有TextView 的新值,但这个活动不知道这个TextViewTextView 在文件 fragment_main.xml 中。片段类包含在MainActivity.java 文件中,就像内部静态类一样。它是 Eclipse 中默认生成的带有片段的活动。你能帮帮我吗?

public class MainActivity extends FragmentActivity {

    ... 

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

        ...

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
        }
    }

    public static class PlaceholderFragment extends Fragment {
        public PlaceholderFragment() { } 

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            return rootView;
        }
    }
}

activity_main.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.zonemedia.skener.MainActivity"
    tools:ignore="MergeRootFrame" />

fragment_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/viewmoje"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:baselineAligned="false"
    android:orientation="vertical"
    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="com.zonemedia.skener.MainActivity$PlaceholderFragment" >

    <TextView
        android:id="@+id/texturl"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/text_url" />
</LinearLayout>

编辑 1: 我试图在 Fragment 中创建公共方法(=setter)并从 FragmentActivity 调用它。 我还尝试直接从片段更改文本: TextView textUrl = (TextView) getView().findViewById(R.id.texturl); textCode.setText("random");

我尝试了@masmic_87 写的内容。在我定义的片段中 TextView fragmentTextView = (TextView)getView().findViewById(R.id.texturl);

然后在活动中我输入了您的代码。应用仍然崩溃:

java.lang.RuntimeException: Unable to start activity ComponentInfo{.....MainActivity}: java.lang.NullPointerException caused by line 86: ((PlaceholderFragment) fragment).fragmentTextView.setText("something");

最后我尝试直接从静态片段调用静态字符串,但 MainActivity.this 带有下划线,原因是:静态字段 MainActivity.this 应该以静态方式访问,并且在范围内无法访问 MainActivity 类型的封闭实例。

我还尝试将内部类 PlaceholderFragment 移动到新的 .java 文件,现在它不是静态的。我尝试了这里的所有建议,但没有任何效果

已解决 我不知道我的头在哪里。在片段中,我调用了 (TextView) getView().... 而不是

   View rootView = inflater.inflate(R.layout.fragment_main, container,false);
   mTextView = (TextView) rootView.findViewById(R.id.texturl);
   mTextView.setText("new text");

【问题讨论】:

  • 你已经尝试过什么?
  • 你应该在你的活动中引用你的片段。
  • 我尝试在 Fragment 中创建公共方法(=setter)并从 FragmentActivity 调用它。我还尝试直接从片段中更改文本:TextView textUrl = (TextView) getView().findViewById(R.id.texturl); textCode.setText("random");
  • @Brano_SR 请使用该代码编辑您的问题,以便您可以正确格式化它。这将使其他人更容易阅读。

标签: java android xml android-fragments textview


【解决方案1】:

看看这是否适合你。

在活动中引用您的片段:

PlaceholderFragment fragment = new PlaceholderFragment(); 

然后在片段的 textview 上定义你想要的文本:

((PlaceholderFragment)fragment).fragmentTextView.setText(text you want);

这将在任何时候将文本视图的值传递给片段。如果你想在调用片段事务的那一刻传递这个值,最好这样:

Bundle bundle = new Bundle();
bundle.putString("textview", "new_value");
PlaceholderFragment fragment= new PlaceholderFragment();
fragment.setArguments(bundle);
transaction.add(R.id.container, fragment);
transaction.commit();

【讨论】:

  • 它不起作用。在片段中我定义了TextView fragmentTextView = (TextView)getView().findViewById(R.id.texturl);,然后在活动中我把你的代码。应用程序仍然崩溃:java.lang.RuntimeException: Unable to start activity ComponentInfo{.....MainActivity}: java.lang.NullPointerException 由第 86 行引起:((PlaceholderFragment) fragment).fragmentTextView.setText("something");
【解决方案2】:

如果片段是活动的静态内部类,那么为什么不将文本存储在活动中的静态字符串中,然后片段将通过MainActivity.this.stringName直接访问它。

此解决方案将使您的代码高度耦合,但如果它是一个静态内部类,并且无论如何它们都必须像这样传递数据,那么您就不必担心这一点。

【讨论】:

  • 它不起作用。 MainActivity.this 带有下划线,原因是:静态字段 MainActivity.this 应以静态方式访问,并且 MainActivity 类型的封闭实例在范围内不可访问。我还尝试将内部类移动到新的 .java 文件,现在它不是静态的。我尝试了这里的所有建议,但没有任何效果。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多