【发布时间】:2014-08-24 17:06:27
【问题描述】:
已解决
我想将新文本设置为TextView。我在MainActivity 中有TextView 的新值,但这个活动不知道这个TextView。 TextView 在文件 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