0

I have a problem with my Android application. If I use setText() method for a textView I have problem like: The Application has stopped unexpectedly. Please try again.

Code of the application:

public class TC1 extends ActionBarActivity {

private TextView textView;

@Override
protected void onCreate(Bundle savedInstanceState) {


    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tc1);

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


    textView = (TextView)findViewById(R.id.textView);
    textView.setText("Hi"); // wrong line
}
...
}

XML fragment:

<RelativeLayout 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"
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.example.tc.TC1$PlaceholderFragment" >

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="35dp"
    android:layout_marginTop="20dp"
    android:text="Text" />

</RelativeLayout>

3 Answers3

0

Your TextView is part of the Fragment xml but you try to get it from the Activity layout. Move the findViewById() call to the onCreateView() method of your Fragment.

For future questions: Please do not quote "The Application has stopped unexpectedly" instead check the LogCat output for error messages and stack traces. The error message you quoted is the default crashed information for the user, nothing that provides any detailed informations for us developers.

WarrenFaith
  • 57,492
  • 25
  • 134
  • 150
0

add this to your code

View view = inflater.inflate(R.layout.fragment_blank, R.id.container,false);

and change

textView = (TextView)findViewById(R.id.textView);

to this

textView = (TextView)view.findViewById(R.id.textView);

Your code is currently trying to access your main activities view which does not contain your textview. Another option is to access it through your fragment in a similar fashion.

user3331142
  • 1,222
  • 1
  • 11
  • 22
0

It seems that textview is part of fragment not activity_tc1.

So either use textview in activity_tc1 layout. or use textview in onCreateView method of fragment. where fragment_tc1 will be your fragment name.

For example

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

           TextView  textView = (TextView) rootView.findViewById(R.id.textView);
           textView.setText("Hi"); 
            return rootView;
        }
Giru Bhai
  • 14,370
  • 5
  • 46
  • 74