I'm trying to follow the video series on YouTube by "The New Boston". I'm getting an error when trying to display a variable on a toast. Heres' what I did..
First off, I created a textView..
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:id="@+id/CountView"
android:text="Number Value is 0"
/>
Then,
public class MainActivity extends ActionBarActivity {
int Counter;
TextView countView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Counter = 0;
countView= (TextView)findViewById(R.id.CountView);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
public void Button1Click(View view) {
Counter+=1;
Toast.makeText(this, "Added 1", Toast.LENGTH_SHORT).show();
countView.setText(String.valueOf(Counter));
}
It says that the program has stopped working. LogCat Log ->
FATAL EXCEPTION: main java.lang.IllegalStateException: Could not execute method of the activity at android.view.View$1.onClick(View.java:3606) at android.view.View.performClick(View.java:4211) at android.view.View$PerformClick.run(View.java:17446) at android.os.Handler.handleCallback(Handler.java:725) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:153) at android.app.ActivityThread.main(ActivityThread.java:5297) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at android.view.View$1.onClick(View.java:3601) ... 11 more Caused by: java.lang.NullPointerException at com.BreadApplications.program1.MainActivity.Button1Click(MainActivity.java:36) ... 14 more
I just started learning Android.. Any help?