Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android Studio ImageButton引用不工作_Java_Android - Fatal编程技术网

Java Android Studio ImageButton引用不工作

Java Android Studio ImageButton引用不工作,java,android,Java,Android,我的ArrayAdapter有问题 我在选项卡视图的两个片段中列出了两个元素列表 Code of the class: public class AdapterListaInCasa extends ArrayAdapter<String> { private int layout_resource; private List<String> str; private Context mContext; public Adapter

我的ArrayAdapter有问题

我在选项卡视图的两个片段中列出了两个元素列表

Code of the class:


public class AdapterListaInCasa extends ArrayAdapter<String> {

    private int layout_resource;
    private List<String> str;
    private Context mContext;

    public AdapterListaInCasa(Context context, int textViewResourceId){
        super(context, textViewResourceId);
        // TODO Auto-generated constructor stub
    }


    public AdapterListaInCasa(Context context, int layout_resource,List<String> str) {

        super(context, layout_resource, str);
        this.mContext = context;
        this.str = str;
        this.layout_resource = layout_resource;

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        final int mPosition = position;
        LayoutInflater vi;
        vi = LayoutInflater.from(getContext());


        if (convertView == null) {
            convertView = vi.inflate(layout_resource, null);
        }



        TextView nomeElemento = (TextView)convertView.findViewById(R.id.nomeElemento);
        nomeElemento.setText(str.get(mPosition));

        ImageButton btnTerminato = (ImageButton) convertView.findViewById(R.id.terminati);
        btnTerminato.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) { // bottone elimina
                SharedPreferences preferences;
                preferences = PreferenceManager.getDefaultSharedPreferences(getContext());
                SharedPreferences.Editor editor = preferences.edit();
                String lista = "inCasa"+mPosition;
                editor.remove(lista);

                str.remove(mPosition);

                notifyDataSetChanged();


                int i=0;
                String TAG = "daComprare"+i;
                String eliminati = preferences.getString(TAG, "nonTrovato");
                while (eliminati != "nonTrovato"){
                    i++;
                    TAG = "daComprare"+i;
                    eliminati = preferences.getString(TAG, "nonTrovato");
                }
                editor.putString(TAG, str.get(mPosition));
                editor.commit();

            }
        });


        ImageButton btnNNmiPiace = (ImageButton) convertView.findViewById(R.id.nonMiPiace);
        btnNNmiPiace.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) { // bottone elimina
                SharedPreferences preferences;
                preferences = PreferenceManager.getDefaultSharedPreferences(getContext());
                SharedPreferences.Editor editor = preferences.edit();
                String lista = "inCasa"+mPosition;
                editor.remove(lista);

                str.remove(mPosition);

                notifyDataSetChanged();

                int i=0;
                String TAG = "nonMiPiace"+i;
                String nonMiPiace = preferences.getString(TAG, "nonTrovato");
                while (nonMiPiace != "nonTrovato"){
                    i++;
                    TAG = "nonMiPiace"+i;
                    nonMiPiace = preferences.getString(TAG, "nonTrovato");
                }
                editor.putString(TAG, str.get(mPosition));
                editor.commit();

            }
        });
        return convertView;
    }
}




Code of the xml:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="20dp"
        android:id="@+id/nomeElemento"/>

    <ImageButton
        android:id="@+id/terminati"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_margin="10dp"
        android:src="@drawable/ic_carrello" />

    <ImageButton
        android:id="@+id/nonMiPiace"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_margin="10dp"
        android:layout_toLeftOf="@+id/terminati"
        android:layout_toStartOf="@+id/terminati"
        android:src="@drawable/ic_carrello" />

</RelativeLayout>
我收到一个致命错误,表示变量为空:

FATAL EXCEPTION: main
Process: com.giulio.marketrack, PID: 24457
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageButton.setMaxWidth(int)' on a null object reference
at com.giulio.marketrack.AdapterListaInCasa.getView(AdapterListaInCasa.java:65)
奇怪的事实是,唯一的问题可能是ID引用,但是,你怎么看,ID是正确的

有人看到失败了吗


Thanx提前了很多时间

用于将BTINTERMINATO设置为null。所以,你的findViewById失败了。调用FindViewByDy之前是否设置了布局您的堆栈跟踪似乎与代码不匹配。请阅读BaseAdapter并在此处实现该代码。Android Studio如何实现此功能?
if (convertView == null) {
   convertView = = inflater.inflate(R.layout.yourlayout name, null);
}
if (convertView == null) {
   convertView = = inflater.inflate(R.layout.yourlayout name, null);
}