this is the code used in tha java class.it doesnt show any errors in eclipse.the logcat shows null pointer exception and class caste exception
package com.myfirstapp;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
public class MyfirstappActivity extends Activity {
/** Called when the activity is first created. */
int count ;
Button add,sub;
TextView txt;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
count = 0;
add = (Button) findViewById(R.id.add_one);
sub = (Button) findViewById(R.id.sub_one);
txt = (Button) findViewById(R.id.display);
setContentView(R.layout.main);
add.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
count = count + 1;
txt.setText("Your total is " + count);
}
});
sub.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
count = count - 1;
txt.setText("Your total is " + count);
}
});
}
this is the main.xml layout file created. i tried cleaning the project meanwhile.tried using the try catch mechanism.the app s running fine when i commented the button referncing part of the above code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1">
<TextView
android:layout_width="fill_parent"
android:layout_height="60dp"
android:text="@string/hello"
android:id="@+id/display"
android:textSize="40dp"
android:gravity="center" />
<Button
android:text="@string/add1"
android:layout_gravity="center"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:id="@+id/add_one"
android:textSize="40dp"
android:gravity="center">
</Button>
<Button
android:text="@string/sub1"
android:layout_gravity="center"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:id="@+id/sub_one"
android:textSize="40dp"
android:gravity="center">
</Button>
</LinearLayout>
txtisTextView, you are Casting it to button. UseAlso you need set the Layout resource for this. So use,
Edit- You code should be something like,