按钮不在片段中工作没有错误?

时间:2014-03-05 23:02:00

标签: java android android-fragments

另一个新手坚持使用片段/按钮!

我有一个带有一些微调器和一个按钮的片段按钮应该打开一个新的活动,但它什么都不做 - 没有错误或强制关闭什么都没有,任何想法?

感谢您的帮助:)

  package info.androidhive.tabsswipe;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

public class BreakfastFragment extends Fragment 


{

    private Button button;

    @Override

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View v = inflater
                .inflate(R.layout.fragment_breakfast, container, false);


        String[] tables = { "Where are you?", "Take out", "Table 1", "Table 2",
                "Table 3", "Table 4", "Table 5", "Table 6", };
        Spinner spinner = (Spinner) v.findViewById(R.id.table_spinner);
        ArrayAdapter<String> tableadapter = new ArrayAdapter<String>(
                this.getActivity(), android.R.layout.simple_spinner_item,
                tables);

        String[] bagels = { "Pick your Bagel!", "Plain", "Wholemeal", "Seeded", };
        Spinner spinner2 = (Spinner) v.findViewById(R.id.spinner_bageltype);
        ArrayAdapter<String> Bageladapter = new ArrayAdapter<String>(
                this.getActivity(), android.R.layout.simple_spinner_item,
                bagels);

        String[] fillings = { "A tasty filling?", "Bacon", "Sausage",
                "Bacon & Sausage", "Soft Cheese", "Butter" };
        Spinner spinner3 = (Spinner) v.findViewById(R.id.spinner_Filling);
        ArrayAdapter<String> fillingadapter = new ArrayAdapter<String>(
                this.getActivity(), android.R.layout.simple_spinner_item,
                fillings);

        String[] sauce = { "Any Sauce?", "Red", "Brown", "Mayo", "None", };
        Spinner spinner4 = (Spinner) v.findViewById(R.id.spinner_sauce);
        ArrayAdapter<String> sauceadapter = new ArrayAdapter<String>(
                this.getActivity(), android.R.layout.simple_spinner_item, sauce);

        tableadapter
                .setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
        Bageladapter
                .setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
        fillingadapter
                .setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
        sauceadapter
                .setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
        spinner.setAdapter(tableadapter);
        spinner2.setAdapter(Bageladapter);
        spinner3.setAdapter(fillingadapter);
        spinner4.setAdapter(sauceadapter);

         return v;
    }

    public void addListenerOnButton(View v) {
        // Select a specific button to bundle it with the action you want

         Button add = (Button) getView().findViewById(R.id.button1);
            add.bringToFront();
            add.setOnClickListener(new OnClickListener()

         {

            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                case R.id.button1:
                    Intent intent = new Intent(getActivity(), Review.class);
                    startActivity(intent);
                    break;


                }
            }

        });

    }
}

2 个答案:

答案 0 :(得分:1)

需要在addListenerOnButton(v)中添加Fragment onCreateView(......),例如:

 @Override

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

   View v = inflater
            .inflate(R.layout.fragment_breakfast, container, false);

  addListenerOnButton(v);
  ...........
  ..........
 }

然后像下面一样编辑addListenerOnButton(v)

public void addListenerOnButton(View v) {
     Button add = (Button) v.findViewById(R.id.button1);
     add.setOnClickListener(new OnClickListener()
     {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
            case R.id.button1:
                Intent intent = new Intent(getActivity(), Review.class);
                startActivity(intent);
                break;
            }
        }
    });
 }

答案 1 :(得分:0)

onCreateView添加以下内容:

button = v.findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener(){
    @Override
    public void onClick(View v) {
        Intent intent = new Intent(getActivity(), Review.class);
        startActivity(intent);
    }

});

评论您的addListenerOnButton