1

I want to change an textView's text via an Activity, but when I change by this code:

public class MainActivity extends AppCompatActivity {

private TextView txtSave;

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

    txtSave = (TextView)findViewById(R.id.txt_save);;
}

public void onClick(View v) {
    TextView txtSave;
    txtSave = (TextView)findViewById(R.id.txt_save);
    txtSave.setText("something");
}

}

and I got this error:

05-14 13:47:46.835 21949-21949/stv.wordspower E/AndroidRuntime: FATAL EXCEPTION: main
                                                            Process: stv.wordspower, PID: 21949
                                                            java.lang.IllegalStateException: Could not execute method for android:onClick
                                                                at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
                                                                at android.view.View.performClick(View.java:4756)
                                                                at android.view.View$PerformClick.run(View.java:19749)
                                                                at android.os.Handler.handleCallback(Handler.java:739)
                                                                at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                at android.os.Looper.loop(Looper.java:135)
                                                                at android.app.ActivityThread.main(ActivityThread.java:5221)
                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                at java.lang.reflect.Method.invoke(Method.java:372)

Can somebody add me help how can I change dynamically? Fragment code:

public class ProgressFragment extends Fragment {

private View rootView;

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

public static ProgressFragment newInstance() {
    Bundle args = new Bundle();
    ProgressFragment fragment = new ProgressFragment();
    fragment.setArguments(args);
    return fragment;
}

}

The textView is on the ProgressFragment. The ProgressFragment is on the MainActivity's FrameLayout. I just want to modify the TextView's values but I can't. I have tried these solutions, but one did not work.

other informations:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
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="stv.wordspower.MainActivity">

<TextView
    android:text="@string/txtView_welcome"
    android:textSize="30sp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:id="@+id/textView"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:textAlignment="center" />

<Button
    android:text="@string/btn_exit"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/btn_exit"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true" />

<Button
    android:id="@+id/btn_settings"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/btn_settings"
    android:layout_above="@+id/btn_contact"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<Button
    android:id="@+id/btn_contact"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/btn_contact"
    android:layout_above="@+id/btn_exit"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<Button
    android:id="@+id/btn_progress"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentStart="true"
    android:layout_below="@+id/textView"
    android:text="@string/btn_progress" />

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_below="@+id/btn_progress"
    android:layout_above="@+id/btn_settings"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:id="@+id/mainActivityFragmentLayout">
</FrameLayout>

<LinearLayout 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"
tools:context="stv.wordspower.ProgressFragment"
android:orientation="vertical">

<!-- TODO: Update blank fragment layout -->

<TextView
    android:id="@+id/txt_progresses"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/txt_progresses"
    android:textAlignment="center"
    android:textColor="@android:color/background_dark"
    android:textSize="30dp"
    android:textStyle="bold" />

<TextView
    android:id="@+id/txt_save"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/txt_empty"
    android:textAlignment="center"
    android:textSize="30dp"
    android:onClick="onClick"
    android:clickable="true"/>

Community
  • 1
  • 1
papi1992
  • 169
  • 3
  • 15

1 Answers1

1

In onClick method, you can get fragment as following:

public void onClick(View v) {
    yourFrag fragment = (yourFrag)getSupportFragmentManager().findFragmentById(R.id.fragment_id);
}

and in fragment, create a method to update the textView, so that we can call it using fragment object as following:

public void update(String s){
    textView.setText(s);
}

Note: This textView should be defined in your fragment.

and call this from main activity as following:

public void onClick(View v) {
    yourFrag fragment = (yourFrag)getSupportFragmentManager().findFragmentById(R.id.fragment_id);
    fragment.update("foo");
}

And yes! if you are not using support library, just replace getSupportFragmentManager() by getFragmentManager().

Kaushal28
  • 5,377
  • 5
  • 41
  • 72
  • I tried this solution, but I can understand this part of the second row: findFragmentById(R.id.fragment_id); What is fragment_id and where I find? – papi1992 May 14 '17 at 18:42
  • fragment id is the id of the fragment declared in xml. @Pisti i.e ID of `` tag. – Kaushal28 May 14 '17 at 18:47
  • hey it's not in manifest. It should be in one of the layout files, either in mainActivity's layout file or a separate xml layout file. As you said you are using frame layout as container, where is framelayout? `` must be inside it. – Kaushal28 May 14 '17 at 18:56
  • 2
    This is correct. You first find the fragment by fragment ID and then using that fragment instance, you call the update method inside the fragment. – Abhiroj Panwar May 14 '17 at 18:59
  • Fragments are not declared in the manifest, Either you place them using a Container layout or by statically coding. – Abhiroj Panwar May 14 '17 at 19:01
  • Okay! It's not here. Can you post your all layout files by editing your question? – Kaushal28 May 14 '17 at 19:08
  • Okay just post the layouts. – Kaushal28 May 14 '17 at 19:13
  • Where is textView of ID text_save? – Kaushal28 May 14 '17 at 19:19
  • And there is no fragment in your framelayout. It's empty. – Kaushal28 May 14 '17 at 19:20
  • I don't keep the fragment on the fragment layout. I call a function what looks like this: getSupportFragmentManager().beginTransaction() .add(R.id.mainActivityFragmentLayout, ProgressFragment.newInstance()) .commit(); } And the fragment is appear on the layout. The textView whose name is txtSave is on the Fragment. I edited the post with fragment_progress.xml. – papi1992 May 14 '17 at 19:24
  • Okay now in your fragment_process.xml, give ID to your Linear Layout and considering it as fragment ID try my answer. I'm not sure about the results, so let me know when you are done. – Kaushal28 May 14 '17 at 19:27
  • Are you sure for this? In the fragment_progress.xml? Not the actitvity_main.xml's frame_layout? – papi1992 May 14 '17 at 19:38
  • It's not working. I put the with id in the FrameLayout of MainActivity's xml file. The Fragment is immediately but I but in vain I click for the textView, the text is not changing BUT, I did not got error... :D – papi1992 May 14 '17 at 19:53