Mohammad Reza Majid Pour Mohammad Reza Majid Pour - 1 month ago 10
Android Question

why i am getting force close when i want to exit?

is there another code for exit the app?
i dont know why it doesnt working in here it worked for me all the time.
maybe its about my xml file i think its heavy or my code is need to be fixed.

here is my code

package ir.whitegate.guardians;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;

public class MainActivity extends Activity {

Button button;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addListenerOnButton();
getActionBar().hide();

}

public void addListenerOnButton() {

final Context context = this;

button = (Button) findViewById(R.id.buttonstory);

button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {

Intent intent = new Intent(context, Story.class);
startActivity(intent);

}
public void EXIT(View view)
{
finish();
}
});

}
}


and here is my xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
android:background="@drawable/mainsithis">

<RelativeLayout
android:layout_height="82dp"
android:layout_width="match_parent">

<HorizontalScrollView
android:layout_height="wrap_content"
android:layout_width="wrap_content">

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

<Button
android:layout_height="82dp"
android:layout_width="35dp"
android:background="@drawable/toleft"/>

<Button
android:layout_height="match_parent"
android:layout_width="82dp"
android:background="@drawable/bios"/>

<Button
android:layout_height="match_parent"
android:layout_width="82dp"
android:background="@drawable/howtoread"/>

<Button
android:layout_height="match_parent"
android:layout_width="82dp"
android:background="@drawable/about"/>

<Button
android:layout_height="match_parent"
android:layout_width="82dp"
android:background="@drawable/writer"/>

<Button
android:layout_height="match_parent"
android:layout_width="82dp"
android:background="@drawable/rateit"/>

<Button
android:layout_height="match_parent"
android:layout_width="82dp"
android:background="@drawable/moreapps"/>

<Button
android:layout_height="82dp"
android:layout_width="35dp"
android:background="@drawable/toright"/>

</LinearLayout>

</HorizontalScrollView>

</RelativeLayout>

<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_weight="0.25"
android:background="@drawable/rainbow"/>

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

<Button
android:id="@+id/buttonstory"
android:layout_height="65dp"
android:layout_width="wrap_content"
android:background="@drawable/starttoread"
android:layout_weight="1.0"/>

<Button
android:layout_height="65dp"
android:layout_width="wrap_content"
android:background="@drawable/exit"
android:layout_weight="1.0"
android:onClick="EXIT"/>

</LinearLayout>



Answer

You should move EXIT method to class from button clicklistener.

Change addListenerOnButton method like below method

public void addListenerOnButton() {

final Context context = this;

button = (Button) findViewById(R.id.buttonstory);

button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            Intent intent = new Intent(context, Story.class);
            startActivity(intent);

        }

    });

}

public void EXIT(View view) 
        {
            finish();
        }