0

I'm having trouble with this part of my application:

            _btnSubmit.setOnClickListener(
                new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // do something...                      
                    }                   
                });



I get the "Unfortunately [app] has stopped working" error.
I can't find the problem.

Here's my xml:

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

    <TextView
        android:id="@+id/txtAppTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="27dp"
        android:text="@string/app_title"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/lblInputHeight"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/lblInputWeight"
        android:layout_below="@+id/lblInputWeight"
        android:layout_marginTop="34dp"
        android:text="@string/input_height"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <Button
        android:id="@+id/btnSubmit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/lblInputHeight"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="46dp"
        android:text="@string/btn_getBMI" />

    <EditText
        android:id="@+id/txtInputHeightFt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/txtInputHeightIn"
        android:layout_alignBottom="@+id/txtInputHeightIn"
        android:layout_toLeftOf="@+id/txtInputHeightIn"
        android:ems="3"
        android:inputType="numberDecimal" />

    <TextView
        android:id="@+id/txtResult"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/lblInputHeight"
        android:layout_alignRight="@+id/txtInputHeightIn"
        android:layout_below="@+id/btnSubmit"
        android:layout_marginTop="59dp"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/lblInputWeight"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/txtAppTitle"
        android:layout_marginTop="60dp"
        android:layout_toLeftOf="@+id/btnSubmit"
        android:text="@string/input_weight"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <EditText
        android:id="@+id/txtInputWeight"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/lblInputHeight"
        android:layout_toLeftOf="@+id/txtInputHeightIn"
        android:ems="3"
        android:inputType="numberDecimal" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/txtInputHeightIn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/btnSubmit"
        android:layout_alignParentRight="true"
        android:ems="3"
        android:inputType="numberDecimal" />

</RelativeLayout>



and activity:

package com.example.droidone;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.os.Build;
import android.view.View.OnClickListener;

public class MainActivity extends ActionBarActivity {

    Button _btnSubmit;
    EditText _txtInputWeight;
    EditText _txtInputHeightFt;
    EditText _txtInputHeightIn;
    TextView _txtResult;

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

        setUpView();
        setUpOnClick();

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

    private void setUpView()
    {
        _btnSubmit = (Button)findViewById(R.id.btnSubmit);
        _txtInputWeight = (EditText)findViewById(R.id.txtInputWeight);
        _txtInputHeightFt = (EditText)findViewById(R.id.txtInputHeightFt);
        _txtInputHeightIn = (EditText)findViewById(R.id.txtInputHeightIn);
        _txtResult = (TextView)findViewById(R.id.txtResult);
    }

    private void setUpOnClick() {

        _btnSubmit = (Button)findViewById(R.id.btnSubmit);
        _btnSubmit.setOnClickListener(
                new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // do something...                      
                    }                   
                });
    }   


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

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

}
Administrateur
  • 891
  • 4
  • 13
  • 29
  • Check the stack trace in your [_logcat_](http://stackoverflow.com/a/12769815/645270). It'll give you the name of the error, and the line which caused it. – keyser May 09 '14 at 21:29

3 Answers3

0

The problem is that you are trying to add a fragment to R.id.container in the onCreate() method. Your layout does not have this element in it. Remove that code and it should do the trick.

Jorgesys
  • 124,308
  • 23
  • 334
  • 268
KMLong
  • 8,345
  • 2
  • 16
  • 19
0

I bet the layout xml is related to the Fragment and not the Activity, try to move both setUpView() and setUpOnClick() into the PlaceholderFragment, like this:

public static class PlaceholderFragment extends Fragment {

    Button _btnSubmit;
    EditText _txtInputWeight;
    EditText _txtInputHeightFt;
    EditText _txtInputHeightIn;
    TextView _txtResult;

    public PlaceholderFragment() {
    }

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

    private void setUpView(View rootView)
    {
        _btnSubmit = (Button)rootView.findViewById(R.id.btnSubmit);
        _txtInputWeight = (EditText)rootView.findViewById(R.id.txtInputWeight);
        _txtInputHeightFt = (EditText)rootView.findViewById(R.id.txtInputHeightFt);
        _txtInputHeightIn = (EditText)rootView.findViewById(R.id.txtInputHeightIn);
        _txtResult = (TextView)rootView.findViewById(R.id.txtResult);
    }

    private void setUpOnClick(View rootView) {

        _btnSubmit = (Button)rootView.findViewById(R.id.btnSubmit);
        _btnSubmit.setOnClickListener(
            new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // do something...                      
                }                   
            });
    }   
}

And just as a piece of advice, try looking into ButterKnife, makes life much easier dealing with injecting views and handling button clicks.

Edit:

Seeing the other answer, I failed to spot that the xml actually is related to the activity. I do think that it is preferred to have all the layout contained in fragments nowadays though. In your case, not doing so leaves the PlaceholderFragment of no use and could might as well be removed entirely.

cYrixmorten
  • 7,110
  • 3
  • 25
  • 33
0

OK you have the elements in your activity_main.xml layout

but when you do a transaction to PlaceholderFragment() it loads fragment_main.xml and the reference of that elements doesn´t exist!

change your PlaceholderFragment class to:

public static class PlaceholderFragment extends Fragment {

    Button _btnSubmit;
    EditText _txtInputWeight;
    EditText _txtInputHeightFt;
    EditText _txtInputHeightIn;
    TextView _txtResult;

    public PlaceholderFragment() {
    }

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

    private void setUpView(View rootView){
        _btnSubmit = (Button)rootView.findViewById(R.id.btnSubmit);
        _txtInputWeight = (EditText)rootView.findViewById(R.id.txtInputWeight);
        _txtInputHeightFt = (EditText)rootView.findViewById(R.id.txtInputHeightFt);
        _txtInputHeightIn = (EditText)rootView.findViewById(R.id.txtInputHeightIn);
        _txtResult = (TextView)rootView.findViewById(R.id.txtResult);
    }

    private void setUpOnClick(View rootView) {
        _btnSubmit = (Button)rootView.findViewById(R.id.btnSubmit);
        _btnSubmit.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // do something :)
                    }                   
                });
    }   

}
Jorgesys
  • 124,308
  • 23
  • 334
  • 268