Reputation: 81
SOLVED
I would like to set new text to TextView
. I have new value of TextView
in MainActivity
but this activity doesn't know this TextView
. TextView
is in the file fragment_main.xml
. Fragment class is included in MainActivity.java
file like inner static class. It was default generated activity with fragment in Eclipse. Can you help me?
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>
EDIT 1:
I tried to make public method (=setter) in Fragment and call it from FragmentActivity.
I also tried to change text directly from fragment:
TextView textUrl = (TextView) getView().findViewById(R.id.texturl);
textCode.setText("random");
I tried what @masmic_87 wrote. In fragment I defined
TextView fragmentTextView = (TextView)getView().findViewById(R.id.texturl);
and then in the activity I putted your code. App is still crashing:
java.lang.RuntimeException: Unable to start activity ComponentInfo{.....MainActivity}: java.lang.NullPointerException caused by line 86: ((PlaceholderFragment) fragment).fragmentTextView.setText("something");
Last I tried call static String directly from static fragment but MainActivity.this is underline with reason: The static field MainActivity.this should be accessed in a static way and No enclosing instance of the type MainActivity is accessible in scope.
I also tried moved inner class PlaceholderFragment to new .java file and now it is not static. I tried all recommendations from here and nothing is working
SOLVED I dont know where I had my head. In fragment I called (TextView) getView().... instead of
View rootView = inflater.inflate(R.layout.fragment_main, container,false);
mTextView = (TextView) rootView.findViewById(R.id.texturl);
mTextView.setText("new text");
Upvotes: 1
Views: 4432
Reputation: 33
Add this to the onCreate(). You can use a binding to setContentView().
ActivityMainBinding binding =
ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
textviewname = findViewById(R.id.textviewid);
textviewname.setText("some text");
Upvotes: -1
Reputation: 7800
if the fragment is a static innner class of the activity, then why don't you just store the text in a static string in the activity and then the fragment will have direct access to it through MainActivity.this.stringName.
This solution will make you code pretty highly coupled but if its a static inner class and they have to pass data around like this anyway, then you don't really have to worry about that.
Upvotes: 1
Reputation: 3574
Have a look if this works for you.
Make reference to your fragment in the activity:
PlaceholderFragment fragment = new PlaceholderFragment();
Then define the text you want on your fragment's textview:
((PlaceholderFragment)fragment).fragmentTextView.setText(text you want);
This would be to pass the textview's value to the fragment in any moment. If you want to pass this value at the moment you call the fragment transaction, it would be better to do it this way:
Bundle bundle = new Bundle();
bundle.putString("textview", "new_value");
PlaceholderFragment fragment= new PlaceholderFragment();
fragment.setArguments(bundle);
transaction.add(R.id.container, fragment);
transaction.commit();
Upvotes: 1