Reputation: 168
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"/>
Upvotes: 1
Views: 2601
Reputation: 5565
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()
.
Upvotes: 1