-1

i am new in android app development via Eclipse...I made a very simple app using 2 buttons and a text view...such that if i click Add Button...text view will add a number (like 0 to 1, 1 to 2 and so on..) similarly, if i click subtract button it will decrease the number.. There is no error in Java and XML file but when i am trying to run it shows an error --- The Application has stopped unexpectedly.Please try again. Force Close...

What should i do to make it run properly.. Expecting a quick and reliable answer...Thanks. Below is my java as well as xml coding !!!

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="@drawable/bg" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="@string/text"
        android:textColor="#fff"
        android:textSize="26sp" />

    <Button
        android:id="@+id/button1"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="@string/badd"
        android:textColor="#ffffff" />

    <Button
        android:id="@+id/button2"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="@string/bsub"
        android:textColor="#ffffff" />

</LinearLayout>



public class MainActivity extends ActionBarActivity {

int count;
Button sum, sub ;
TextView display ;

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

    count = 0;
    sum = (Button) findViewById(R.id.button1);
    sub = (Button) findViewById(R.id.button2);
    display = (TextView) findViewById(R.id.textView1);

    sum.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            count++;
            display.setText("Your Total Count is " + count);            }
    });

    sub.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            count++;
            display.setText("Your Total Count is " + count);            }
    });

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

3 Answers3

0

Try this please

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_main, container,
  false);

sum = (Button) root.findViewById(R.id.button1);
sub = (Button) root.findViewById(R.id.button2);
display = (TextView) root.findViewById(R.id.textView1);

sum.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        count++;
        display.setText("Your Total Count is " + count);            
 }
});

// the rest of your code

return root;
}
Farouk Touzi
  • 3,451
  • 2
  • 19
  • 25
  • i did...bt problem still there – Rubal May 23 '14 at 10:49
  • You have a NullPointerException in your MainActivity (line 30). Can you now please put you java code ? – Farouk Touzi May 23 '14 at 10:56
  • check above... i edited above in my question,,,java code is there now – Rubal May 23 '14 at 11:00
  • 1
    I assume that the view is in the fragment layout (fragment_main.xml) and not in the activity layout (activity_main.xml). Like explained already, the method onCreate() is too early in the lifecycle to find it in the activity view hierarchy, and a null is returned. Invoking a method on null causes the NPE. Move the code in this case to the fragment onCreateView(). I edited my response above. – Farouk Touzi May 23 '14 at 11:30
0

If you use your button or button click event etc. you need to get resources. For example, your button id is button1:

public class MainActivity extends Activity {


    Button button1;


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


        button1 = (Button)findViewById(R.id.button1);

....

}

}

I think you don't use findViewById method. Can you please give your Java code?

CanDroid
  • 633
  • 6
  • 15
0

As you've not put your JAVA file and unable to post logcat, I'm showing the similar APP I've developed couple of weeks back with the same/similar concept. Please read through..

Simple_Maths Activity

public class Simple_Maths extends Activity {
    TextView tv_Output;
    Button Addition, Subtract, ClearTot;
    int counter = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView (R.layout.maths_activity);
        tv_Output = (TextView) findViewById(R.id.tv_Display);
        Addition = (Button) findViewById(R.id.btn_Add);
        Subtract = (Button) findViewById(R.id.btn_Sub);
        ClearTot = (Button) findViewById(R.id.btn_Clear);


        Addition.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                counter++;
                tv_Output.setText("Your Total is : " + counter);
            }
        });

        Subtract.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                counter--;
                if (counter < 0) {
                    counter = 0;
                }
                tv_Output.setText("Your Total is : " + counter);
            }
        });

        ClearTot.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                counter = 0;
                tv_Output.setText("Your Total is cleared");
            }
        });
    }


}

maths_activity.xml file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:background="@color/LightOrange"
    android:orientation="vertical"
    android:padding="10dp" >

    <TextView
        android:id="@+id/tv_Display"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:background="@color/Variety"
        android:gravity="center"
        android:hint="@string/tv_Display"
        android:textSize="35sp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/LightOrange"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btn_Add"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="12dp"
            android:layout_marginTop="10dp"
            android:background="@color/Red"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:text="@string/btn_Add"
            android:textSize="20sp" />

        <Button
            android:id="@+id/btn_Sub"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp"
            android:background="@color/LightYellow"
            android:text="@string/btn_Sub"
            android:textSize="20sp" />

        <Button
            android:id="@+id/btn_Clear"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp"
            android:background="@color/LightGreen"
            android:text="@string/btn_Clear"
            android:textSize="20sp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/Light_Green"
            android:layout_marginTop="60dp"
            android:padding="10dp"
            android:gravity="center"
            android:text="Hello, Mr. This is in Portrait Mode :>)" />
    </LinearLayout>

</LinearLayout>

Ensure to have it declared in the manifest file

Let me know if any issues in understanding

Ramakishna Balla
  • 1,020
  • 1
  • 8
  • 12