0

I have checked the posts. And here is the checklist of what I have tried

  1. Make sure that a TextView by that name exists
  2. That all functions are called after setContentView() in onCreate()

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??

Manish
  • 11
  • 6
  • Either you are not showing the whole code for method sellBill or you need to clean your project because maybe the id variables got messed up... – Martin Cazares Mar 09 '14 at 04:18
  • Are you sure the sellBill method is not called from somewhere else? The method is public so it might be possible it is called prior setContentView execution. – Konstantin Burov Mar 09 '14 at 04:25
  • Are you sure the resource string exists as you have it referenced? Have you tried setting the text with a string literal? – cjbrooks12 Mar 09 '14 at 06:57
  • @MartinCazares: The rest of the code is autogenerated. I am using Android Studio. Could it be related to that? – Manish Mar 11 '14 at 06:17
  • @KonstantinBurov: I built this to narrowdown to where the error was generated. sellBill is defined and used only once. – Manish Mar 11 '14 at 06:21
  • @cjbrooks12: I tried that, still fails. :( – Manish Mar 11 '14 at 06:23
  • Please post your layout – zgc7009 Mar 14 '14 at 20:08
  • @zgc7009 Sorry about that. Added now. – Manish Mar 14 '14 at 20:18
  • To answer your question at the bottom of your edit, yes. If your fragment_main.xml really is the top one, and main.xml is the bottom one, then you need to switch the code that is in each one. Basicall you need to make main.xml the top one and fragment_main.xml the bottom one. – zgc7009 Mar 14 '14 at 20:20

1 Answers1

0

Just so this question has an answer, you need to make sure that your variables get defined in the layout that you have set in your onCreate method, in this case setContentView(R.layout.activity_main.xml); Since you have set your activity_main.xml as your content view for your Activity, you need to make sure that layout actually contains the views and id's you are trying to use. All you need to do is move the code from your fragment layout to your main layout and vice versa. Reason being, if a particular layout is not set as your content view, the content from that layout hasn't actually been loaded into your Activity to be referenced to.

If you are really intent on using the fragment, simply change this:

tDummy=(TextView)findViewById(R.id.textViewDummy);
tDummy.setText("Testing.");

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

to

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

tDummy=(TextView)findViewById(R.id.textViewDummy);
tDummy.setText("Testing.");

And it should work, since now your fragment has been loaded into your container

zgc7009
  • 3,371
  • 5
  • 22
  • 34
  • I think I found an approximate answer here: http://stackoverflow.com/questions/21741686/android-studio-cant-access-objects-in-fragment-main-xml – Manish Mar 14 '14 at 20:26
  • It says that nothing should be defined in the activity_main.xml only the access and def for the variables should be changed to the class def that extends fragments. – Manish Mar 14 '14 at 20:27
  • I don't know if that question directly relates to your question. He intentionally is utilizing his fragments, you don't really even need to use the fragment. It is far more than acceptable to define things inside of activity_main.xml. I can almost guarantee you that if you simply switch those files you will be completely fine. – zgc7009 Mar 14 '14 at 20:29
  • Another solution to your problem, if you really want to use fragment, check my updated answer. – zgc7009 Mar 14 '14 at 20:30