I have checked the posts. And here is the checklist of what I have tried
- Make sure that a
TextView
by that name exists - That all functions are called after
setContentView()
inonCreate()
It still continues to baffle me. Your help your be appreciated
Please note that this activity (for which the code is given) is trigger from a parent (as given in the Android Manifest snippet below).
The java code for the activity is: (This code has been changed to simplify the question)
public class MainActivity extends ActionBarActivity {
private TextView tDummy;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tDummy=(TextView)findViewById(R.id.textViewDummy);
tDummy.setText("Testing.");
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
...
public void changeMsg(View view){
TextView dummy=(TextView)findViewById(R.id.textViewDummy);
dummy.setText("Changed!");
}
Why am I still getting a null pointer exception?! It works fine when I click the button (which invokes the changeMsg function), but that just circumvents the problem.
I am using android studio.
XML file (There are two: activity_main.xml and fragment_main.xml) fragment_main.xml is as follows:
<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="mk.app.MainActivity$PlaceholderFragment">
<TextView
android:text="@string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textViewDummy" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textViewDummy"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="42dp"
android:layout_marginTop="26dp"
android:onClick="changeMsg"/>
</RelativeLayout>
main_activity.xml is:
<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="mk.app.MainActivity"
tools:ignore="MergeRootFrame" />
Could it be an issue with having using the fragment layout? As the content is set to activity_main, but the definitions are in fragment_main.xml??