如何设置按钮单击?

时间:2014-05-09 21:26:17

标签: android eclipse

我在申请的这一部分遇到了问题:

            _btnSubmit.setOnClickListener(
                new View.OnClickListener() {

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


我得到"不幸的是[app]已停止工作" 错误。
我无法找到问题点。

这是我的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>



活动:

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;
        }
    }

}

3 个答案:

答案 0 :(得分:0)

问题是您尝试在R.id.container方法中向onCreate()添加片段。您的布局中没有此元素。删除该代码,它应该做到这一点。

答案 1 :(得分:0)

我打赌布局xml与Fragment有关,而不是Activity,尝试将setUpView()和setUpOnClick()移动到PlaceholderFragment中,如下所示:

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...                      
                }                   
            });
    }   
}

就像一条建议一样,尝试调查ButterKnife,让生活更容易处理注入视图和处理按钮点击。

编辑:

看到另一个答案,我没有发现xml实际上与活动有关。我认为现在最好将片段中的所有布局包含在片段中。在您的情况下,不这样做会使PlaceholderFragment无用,也可能完全删除。

答案 2 :(得分:0)

好的,您的activity_main.xml布局

中有元素

但当您对PlaceholderFragment()进行交易时,它会加载fragment_main.xml并且该元素的引用不存在!

将您的PlaceholderFragment课程更改为:

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 :)
                    }                   
                });
    }   

}