Vishal Sharma
Vishal Sharma

Reputation: 29

Android Application closing for no reason

Hi guys i started learning android development about 2 days ago.. So was trying out with a basic application to increment and decrement. Everything is okay but when i'm testing the application even on the emulator, i get a message "Unfortunately, application " I tried debugging the code and found that Even after declaring a statement findViewById(), my variables are pointing to null. And i think that's the reason for this error.. heres my code, in MainActivity.java. Please help me out!

public class MainActivity extends ActionBarActivity {

/* Your original code */
/*
TextView sum;
Button increment;
Button decrement;
*/
int total;


/* My interpretation */
protected static TextView sum;
protected static Button increment;
protected static Button decrement;

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

    this.findViewById();

    total = 0;

    increment.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            total++;
            sum.setText("Sum is "+total);
        }
    });

    decrement.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            total--;
            sum.setText("Sum is "+total);
        }
    });

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

/* Your original code (moved to fragment) */
/*
private void findViewById() {
    sum = (TextView) findViewById(R.id.tv);          // sum points to null
    increment = (Button) findViewById(R.id.inc);  //increment points to null
    decrement = (Button) findViewById(R.id.dec);  // decrement points to null
}
*/

EDIT ::

placeholder fragment ::

 public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    /* added */
    Context ctx = null;

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

        /* added */
        init();
    }

    /* added */
    private void init()
    {
    final View v = getView();

    ctx = getActivity().getApplicationContext();

        MainActivity.sum = (TextView) v.findViewById(R.id.tv);          // sum points to null
        MainActivity.increment = (Button) v.findViewById(R.id.inc);  //increment points to null
        MainActivity.decrement = (Button) v.findViewById(R.id.dec);  // decrement points to null
    }
}

here is the layout ::

<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.vishal.Simplecalc.MainActivity$PlaceholderFragment">

<TextView
    android:id="@+id/tv"
    android:text="@string/sumView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="30sp"
    android:layout_gravity="center"
    tools:context=".MainActivity"
/>

<Button
    android:id="@+id/inc"
    android:text="@string/inc"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:textSize="30sp"
    android:layout_gravity="center"
    android:textStyle="bold"
    tools:context=".MainActivity"
    />

<Button
    android:id="@+id/dec"
    android:text="@string/dec"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:textSize="30sp"
    android:layout_gravity="center"
    android:textStyle="bold"
    tools:context=".MainActivity"
    />
</LinearLayout>

Upvotes: 1

Views: 96

Answers (3)

Berkan Denizyaran
Berkan Denizyaran

Reputation: 11

Sometimes it can be because of emulators, create new emulator or try it with your real device .

Upvotes: 0

DigCamara
DigCamara

Reputation: 5568

Just guessing here (you haven't added the name for your xml layout).

I'm guessing you're probably confusing your layouts and just posted fragment_main.xml which actually has the content your code is believing to be held in activity_main, which causes your code to return nulls when you're assigning the findViewById()

Upvotes: 1

ElectronicGeek
ElectronicGeek

Reputation: 3320

I think the problem is that PlaceholderFragment is setting a new layout, and that doesn't allow the text to be changed.

Upvotes: 0

Related Questions