-1

I am little new to Android Programming but I am very familiar with Java and C++. Following is my Java code of my app it works on toast but when I tried to change text it causes "Your App has stopped working".

Code

public class MainActivity extends ActionBarActivity {

int counter;
Button sum, sub;
TextView display;

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

    counter = 0;
    display = (TextView) findViewById(R.id.show);
    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container,
                false);
        return rootView;
    }
}
public void addButtonClicked(View v) {
     counter++;
     tToast("Your Total Is : " + counter);
        display.setText("Your Total Is : " + counter);

    }
public void subButtonClicked(View v) {
     counter--;
     tToast("Your Total Is : " + counter);
        display.setText("Your Total Is : " + counter);

    }
 private void tToast(String s) {
        Context context = getApplicationContext();
        int duration = Toast.LENGTH_LONG;
        Toast toast = Toast.makeText(context, s, duration);
        toast.show();
    }
}

StackTrace :

07-05 05:22:42.888: E/AndroidRuntime(1650): FATAL EXCEPTION: main
07-05 05:22:42.888: E/AndroidRuntime(1650): Process: com.example.counter, PID: 1650
07-05 05:22:42.888: E/AndroidRuntime(1650): java.lang.IllegalStateException: Could not execute method of the activity
07-05 05:22:42.888: E/AndroidRuntime(1650):     at android.view.View$1.onClick(View.java:3823)
07-05 05:22:42.888: E/AndroidRuntime(1650):     at android.view.View.performClick(View.java:4438)
07-05 05:22:42.888: E/AndroidRuntime(1650):     at android.view.View$PerformClick.run(View.java:18422)
07-05 05:22:42.888: E/AndroidRuntime(1650):     at android.os.Handler.handleCallback(Handler.java:733)
07-05 05:22:42.888: E/AndroidRuntime(1650):     at android.os.Handler.dispatchMessage(Handler.java:95)
07-05 05:22:42.888: E/AndroidRuntime(1650):     at android.os.Looper.loop(Looper.java:136)
07-05 05:22:42.888: E/AndroidRuntime(1650):     at android.app.ActivityThread.main(ActivityThread.java:5017)
07-05 05:22:42.888: E/AndroidRuntime(1650):     at java.lang.reflect.Method.invokeNative(Native Method)
07-05 05:22:42.888: E/AndroidRuntime(1650):     at java.lang.reflect.Method.invoke(Method.java:515)
07-05 05:22:42.888: E/AndroidRuntime(1650):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-05 05:22:42.888: E/AndroidRuntime(1650):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-05 05:22:42.888: E/AndroidRuntime(1650):     at dalvik.system.NativeStart.main(Native Method)
07-05 05:22:42.888: E/AndroidRuntime(1650): Caused by: java.lang.reflect.InvocationTargetException
07-05 05:22:42.888: E/AndroidRuntime(1650):     at java.lang.reflect.Method.invokeNative(Native Method)
07-05 05:22:42.888: E/AndroidRuntime(1650):     at java.lang.reflect.Method.invoke(Method.java:515)
07-05 05:22:42.888: E/AndroidRuntime(1650):     at android.view.View$1.onClick(View.java:3818)
07-05 05:22:42.888: E/AndroidRuntime(1650):     ... 11 more
07-05 05:22:42.888: E/AndroidRuntime(1650): Caused by: java.lang.NullPointerException
07-05 05:22:42.888: E/AndroidRuntime(1650):     at com.example.counter.MainActivity.addButtonClicked(MainActivity.java:104)
07-05 05:22:42.888: E/AndroidRuntime(1650):     ... 14 more
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
Wasif Khan
  • 956
  • 7
  • 25

1 Answers1

1

This is because findViewById() searches in the activity_main layout, while the TextView is located in the fragment's layout fragment_main.

Move that piece of code in the onCreateView() method of the fragment:

//...
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
display = (TextView)rootView.findViewById(R.id.show);

Notice that now you access it through rootView view:

display = (TextView)rootView.findViewById(R.id.show);

otherwise you would get again NullPointerException.

Lal
  • 14,726
  • 4
  • 45
  • 70
  • When I moved it.. It caused error Cannot make a static reference to the non-static method findViewById(int) from the type Activity – Wasif Khan Jul 05 '14 at 09:53
  • I've dited my answer..Try it once more.. @user3125340 – Lal Jul 05 '14 at 10:06
  • @Lal this is still wrong. You have the initialization before inflating the layout. Also the button should be initialized in onCreateview and op should remove` android:onClick="addButtonClicked"` and have a click listener in fragment – Raghunandan Jul 05 '14 at 11:16
  • Sorry..That was a mistake...I've edited my answer.. – Lal Jul 05 '14 at 11:18
  • @Lal still wrong `display` cannot be accessed in activity. Notice `display.setText("Your Total Is : " + counter);` in activity – Raghunandan Jul 05 '14 at 11:19
  • Is it ok now...?? @Raghunandan – Lal Jul 05 '14 at 11:29