Java 如何在一个类中使用来自不同xml文件的按钮

Java 如何在一个类中使用来自不同xml文件的按钮,java,android,xml,Java,Android,Xml,我有一个包含多个项目的列表视图和一个启动新活动的按钮。我运行应用程序时出现空指针异常。如何以及在何处设置OnClickListener以正常运行 还有,我怎样才能有目的地通过名为listaIngrediente的arraylist 这是我的密码 main_activity.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/a

我有一个包含多个项目的列表视图和一个启动新活动的按钮。我运行应用程序时出现空指针异常。如何以及在何处设置OnClickListener以正常运行

还有,我怎样才能有目的地通过名为listaIngrediente的arraylist

这是我的密码 main_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context="com.example.radu.fridgecheck.MainActivity"
    android:orientation="vertical">

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1">
    </ListView>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/find_recipies"
        android:id="@+id/find"
        android:layout_weight="0"/>
</LinearLayout>

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="6dip" >

    <CheckBox
        android:id="@+id/checkBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:focusable="false"
        android:focusableInTouchMode="false"/>

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/checkBox"
        android:layout_alignBottom="@+id/checkBox"
        android:layout_toRightOf="@+id/checkBox"
        android:text="TextView" />  
</RelativeLayout>

适配器

    public class Adapter extends ArrayAdapter<Ingredient> {

    public LayoutInflater inflater;
    public ArrayList<Ingredient> listaIngrediente;
    ArrayList<String> ingredienteDisponibile;
    private Activity activity;


    public Adapter(Activity activity, int textResourceId, ArrayList<Ingredient> ingrediente) {
        super(activity, textResourceId, ingrediente);
        this.activity=activity;
        this.listaIngrediente=ingrediente;

        try {
            inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public int getCount() {
        return listaIngrediente.size();
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;

        if (convertView == null) {
            v = inflater.inflate(R.layout.list_item, null);
        }

        final Ingredient ingredients = getItem(position);

        final TextView display_ingredients = (TextView) v.findViewById(R.id.name);
        display_ingredients.setText(ingredients.getNameI());

        final CheckBox checkBox = (CheckBox) v.findViewById(R.id.checkBox);

        ListView listView = (ListView) v.findViewById(R.id.listView1);
        Button button = (Button) v.findViewById(R.id.button);
        listView.addFooterView(button);

        Button find = (Button) v.findViewById(R.id.find);
        find.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i = new Intent(getContext(), ShowRecipes.class);
                Bundle args = new Bundle(); /// nu reusesc sa transfer obiectele
                args.putSerializable("ARRAYLIST",listaIngrediente);
                i.putExtra("BUNDLE",args);
                activity.startActivity(i);
            }
        });

        v.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(!ingredients.isSelected()) {
                    checkBox.setChecked(true);
                    ingredients.setSelected(true);
                }
                else{
                    checkBox.setChecked(false);
                    ingredients.setSelected(false);                  
                }
            }
        });
        return v;
    }
}
公共类适配器扩展了ArrayAdapter{
公共场所;充气机;
公共阵列列表;
ArrayList IngrediteDisponibile;
私人活动;
公共适配器(活动活动、int-textResourceId、ArrayList-ingrediente){
超级(活动、textResourceId、ingrediente);
这个。活动=活动;
这个.listaIngrediente=ingrediente;
试一试{
充气器=(LayoutInflater)activity.getSystemService(Context.LAYOUT\u充气器\u SERVICE);
}捕获(例外e){
e、 printStackTrace();
}
}
public int getCount(){
返回listaIngrediente.size();
}
公共长getItemId(int位置){
返回位置;
}
公共视图getView(int位置、视图转换视图、视图组父视图){
视图v=转换视图;
if(convertView==null){
v=充气机充气(R.layout.list_项,空);
}
最终成分=获取项目(位置);
最终文本视图显示内容=(文本视图)v.findViewById(R.id.name);
显示_components.setText(components.getNameI());
final CheckBox=(CheckBox)v.findviewbyd(R.id.CheckBox);
ListView ListView=(ListView)v.findViewById(R.id.listView1);
按钮按钮=(按钮)v.findViewById(R.id.Button);
listView.addFooterView(按钮);
按钮查找=(按钮)v.findViewById(R.id.find);
find.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
Intent i=新的Intent(getContext(),ShowRecipes.class);
Bundle args=new Bundle();///nu reusesc sa transfer obiectele
args.putSerializable(“ARRAYLIST”,listaIngrediente);
i、 putExtra(“捆绑”,args);
活动性、触觉性(i);
}
});
v、 setOnClickListener(新视图。OnClickListener(){
@凌驾
公共void onClick(视图){
如果(!Components.isSelected()){
checkBox.setChecked(true);
成分。选择(正确);
}
否则{
checkBox.setChecked(false);
成分。选择(假);
}
}
});
返回v;
}
}

您必须在MainActivity类中编写,因为您的按钮位于main\u activity.xml

在MainActivity中,编写此方法

public void showRecipes(View view) {

   Intent i = new Intent(getContext(), ShowRecipes.class);
            Bundle args = new Bundle(); /// nu reusesc sa transfer obiectele
            args.putSerializable("ARRAYLIST",listaIngrediente);
            i.putExtra("BUNDLE",args);
            startActivity(i);
}
在Xml中添加onClick

  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/find_recipies"
    android:id="@+id/find"
    android:onClick="showRecipes"
    />`
`
有关单击事件的更多信息

非常感谢,现在我可以使用“查找”按钮,但我仍然无法通过Intent传递Arraylist。您的成分类必须实现可序列化,并且其中的所有变量都必须可序列化。如果它解决了你的问题,就接受我的回答吧。算了,你今天救了我