尝试从字段android小部件textview读取

时间:2016-06-15 07:09:23

标签: android nullpointerexception textview adapter

我在Listview中有一个 CustomAdapter 。 Listview有一个两个Button和Textview。我想通过(+)按钮和( - )按钮在Textview中设置文本。当我点击(+)按钮时, Textview 中的数字会增加。但是当我点击( - )按钮时,这是一个错误。

BtnArtir表示 - >增加| BtnAzalt意味着 - >减少

public View getView(final int position, View view, ViewGroup parent) {    

        if (position == 0) {
            //Slider
            inflater = context.getLayoutInflater();
            rowView4 = inflater.inflate(R.layout.custom_anasayfa_slider, null, true);
            Row4_Tanimlamalar();
            Row4_Fonksiyonlar();
            return rowView4;
        } else if (position == 1) {

            if (KampanyaCinsi.equals("Resim")) {
                inflater = context.getLayoutInflater();
                rowView3 = inflater.inflate(R.layout.custom_anasayfa_kampanya_resim, null, true);
                Row3_Tanimlamalar();
                Row3_Fonksiyonlar();
                return rowView3;
            } else if (KampanyaCinsi.equals("Yazi")) {
                inflater = context.getLayoutInflater();
                rowView2 = inflater.inflate(R.layout.custom_anasayfa_kampanya_yazi, null, true);
                Row2_Tanimlamalar();
                Row2_Fonksiyonlar();
                return rowView2;
            }
        } else if (position == MainActivity.listview.getAdapter().getCount() - 1) {

            inflater = context.getLayoutInflater();
            rowView5 = inflater.inflate(R.layout.custom_site_detay_sayfalari, null, true);
            Row5_Tanimlamalar();
            Row5_Fonksiyonlar();
            return rowView5;
        } else { 
            holder = new Holder();
            inflater = context.getLayoutInflater();
            rowView = inflater.inflate(R.layout.custom_item_anasayfa_urunleri, null, true);
            Row_Tanimlamalar();

            if (urunBirimi.get(position).equals("Adet")) {
                holder.TxtAdetKg.setText("1");
                holder.TxtAdetKgMetin.setText("Adet");
            } else {
                holder.TxtAdetKg.setText("0.25");
                holder.TxtAdetKgMetin.setText("Kg");
            }

            holder.BtnArtir.setTag(holder);
            holder.BtnArtir.setOnClickListener(new View.OnClickListener() {


                @Override
                public void onClick(View v) {
                    Holder mH = (Holder) v.getTag();

                    Toast.makeText(context, String.valueOf(urunBirimi.size()), Toast.LENGTH_SHORT).show();

                    if (urunBirimi.get(position).equals("Adet")) {

                        adet = Integer.parseInt(urunAdedi.get(position)) + 1;
                        urunAdedi.set(position, String.valueOf(adet));                        
                        mH.TxtAdetKg.setText(String.valueOf(adet));

                    } else if (urunBirimi.get(position).equals("Kg")) {

                        kg = Double.parseDouble(urunAdedi.get(position)) + 0.25;
                        urunAdedi.set(position, String.valueOf(kg));             
                        mH.TxtAdetKg.setText(urunAdedi.get(position));

                    } else {
                        Toast.makeText(context, "Beklenmedik bir hata oluştu -", Toast.LENGTH_SHORT).show();
                    }

                }
            });


            holder.BtnAzalt.setTag(holder);
            holder.BtnAzalt.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {

                    Holder mH = (Holder) v.getTag();


                    if (urunBirimi.get(position).equals("Adet")) {

                        if (Integer.parseInt(urunAdedi.get(position)) == 1) {
                        } else {
                            adet = Integer.parseInt(urunAdedi.get(position)) - 1;
                            urunAdedi.set(position, String.valueOf(adet));                           
                            mH.TxtAdetKg.setText(String.valueOf(adet));


                        }
                    } else if (urunBirimi.get(position).equals("Kg")) {

                        if (Double.parseDouble(urunAdedi.get(position)) == 0.25) {
                        } else {
                            kg = Double.parseDouble(urunAdedi.get(position)) - 0.25;
                            urunAdedi.set(position, String.valueOf(kg));                           
                            mH.TxtAdetKg.setText(urunAdedi.get(position));
                        }
                    } else {
                        Toast.makeText(context, "Beklenmedik bir hata oluştu -", Toast.LENGTH_SHORT).show();
                    }
                }
            });


            return rowView;
        }
        return null;
    }

Row_Tanimlamalar

private void Row_Tanimlamalar() {

    holder.ImgUrunResmi = (ImageView) rowView.findViewById(R.id.ImgUrunResmi);
    holder.TxtUrunAdi = (TextView) rowView.findViewById(R.id.TxtUrunAdi);
    holder.TxtAdetKg = (TextView) rowView.findViewById(R.id.TxtAdetKg);
    holder.TxtAdetKgMetin = (TextView) rowView.findViewById(R.id.TxtAdetKgMetin);
    holder.TxtUrunFiyat1 = (TextView) rowView.findViewById(R.id.TxtUrunFiyat1);
    holder.TxtUrunFiyat2 = (TextView) rowView.findViewById(R.id.TxtUrunFiyat2);
    holder.BtnAzalt = (Button) rowView.findViewById(R.id.BtnAzalt);
    holder.BtnArtir = (Button) rowView.findViewById(R.id.BtnArtir);
    holder.BtnSepeteEkle = (Button) rowView.findViewById(R.id.BtnSepeteEkle);
    holder.LayoutUrunGorseli = (LinearLayout) rowView.findViewById(R.id.LayoutUrunGorseli);


}

ViewHolder

public static class Holder {

    public ImageView ImgUrunResmi;
    public TextView TxtUrunAdi;
    public TextView TxtAdetKg;
    public TextView TxtAdetKgMetin;
    public TextView TxtUrunFiyat1;
    public TextView TxtUrunFiyat2;
    public Button BtnAzalt;
    public Button BtnArtir;
    public Button BtnSepeteEkle;
    public LinearLayout LayoutUrunGorseli;
}

布局

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

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/anasayfa_urun_bg"
        android:orientation="vertical">

        <LinearLayout
            android:id="@+id/LayoutUrunGorseli"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/ImgUrunResmi"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:adjustViewBounds="true" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:gravity="center"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/TxtUrunAdi"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="15dp"
                android:layout_marginRight="15dp"
                android:background="@drawable/urun_adi_bg"
                android:gravity="center"
                android:text="Ürün Adı"
                android:textColor="@color/RenkGenelSiteSiyah"
                android:textSize="18dp"
                android:textStyle="bold" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:gravity="center"
            android:orientation="horizontal"
            android:weightSum="100">

            <LinearLayout
                android:id="@+id/LayoutAdetKg"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginRight="5dp"
                android:layout_weight="50"
                android:gravity="center"
                android:orientation="horizontal"
                android:weightSum="100">

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="33"
                    android:gravity="center"
                    android:orientation="horizontal">

                    <Button
                        android:id="@+id/BtnAzalt"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="25"
                        android:background="@drawable/buton_artirma_azaltma_style"
                        android:gravity="center"
                        android:text="-"
                        android:textColor="@color/RenkGenelSiteFiyatRengiAcikSari"
                        android:textSize="25dp"
                        android:textStyle="bold" />
                </LinearLayout>

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="34"
                    android:background="@drawable/adetkg_bg"
                    android:gravity="center"
                    android:orientation="vertical"
                    android:weightSum="100">

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="0dp"
                        android:layout_weight="40"
                        android:gravity="center"
                        android:orientation="horizontal">

                        <TextView
                            android:id="@+id/TxtAdetKg"
                            android:layout_width="0dp"
                            android:layout_height="wrap_content"
                            android:layout_weight="25"
                            android:gravity="center"
                            android:text="1"
                            android:textColor="@color/RenkGenelSiteBeyaz"
                            android:textSize="15dp" />
                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="0dp"
                        android:layout_weight="30"
                        android:gravity="center|bottom"
                        android:orientation="horizontal">

                        <TextView
                            android:id="@+id/TxtAdetKgMetin"
                            android:layout_width="0dp"
                            android:layout_height="wrap_content"
                            android:layout_weight="25"
                            android:gravity="center"
                            android:text="Adet"
                            android:textColor="@color/RenkGenelSiteBeyaz"
                            android:textSize="10dp" />
                    </LinearLayout>
                </LinearLayout>

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="33"
                    android:gravity="center"
                    android:orientation="horizontal">

                    <Button
                        android:id="@+id/BtnArtir"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="25"
                        android:background="@drawable/buton_artirma_azaltma_style"
                        android:gravity="center"
                        android:text="+"
                        android:textColor="@color/RenkGenelSiteFiyatRengiAcikSari"
                        android:textSize="25dp"
                        android:textStyle="bold" />
                </LinearLayout>
            </LinearLayout>

            <RelativeLayout
                android:id="@+id/LayoutFiyat"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_weight="50"
                android:background="@drawable/fiyat_bg"
                android:gravity="center">

                <TextView
                    android:id="@+id/TxtUrunFiyat1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="30,"
                    android:textColor="@color/RenkGenelSiteFiyatRengiAcikSari"
                    android:textSize="22dp"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/TxtUrunFiyat2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentTop="true"
                    android:layout_toEndOf="@+id/TxtUrunFiyat1"
                    android:layout_toRightOf="@+id/TxtUrunFiyat1"
                    android:text="30"
                    android:textColor="@color/RenkGenelSiteFiyatRengiAcikSari"
                    android:textSize="13dp"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/Layout3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignTop="@+id/TxtUrunFiyat2"
                    android:layout_toEndOf="@+id/TxtUrunFiyat2"
                    android:layout_toRightOf="@+id/TxtUrunFiyat2"
                    android:text=" TL"
                    android:textColor="@color/RenkGenelSiteFiyatRengiAcikSari"
                    android:textSize="20dp"
                    android:textStyle="bold" />
            </RelativeLayout>
        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="horizontal">

            <LinearLayout
                android:id="@+id/LayoutSepeteEkle"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"
                android:gravity="center">

                <Button
                    android:id="@+id/BtnSepeteEkle"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:background="@drawable/buton_sepete_ekle"
                    android:text="Sepete Ekle"
                    android:textColor="#ffffff"
                    android:textStyle="bold" />
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

日志

Process: com.mikepenz.materialdrawer.app.debug, PID: 18695
                                                                                   java.lang.NullPointerException: Attempt to read from field 'android.widget.TextView com.mikepenz.materialdrawer.app.adapter.CustomAnasayfaAdapter$Holder.TxtAdetKg' on a null object reference
                                                                                       at com.mikepenz.materialdrawer.app.adapter.CustomAnasayfaAdapter$2.onClick(CustomAnasayfaAdapter.java:232)
                                                                                       at android.view.View.performClick(View.java:5697)
                                                                                       at android.widget.TextView.performClick(TextView.java:10826)
                                                                                       at android.view.View$PerformClick.run(View.java:22526)
                                                                                       at android.os.Handler.handleCallback(Handler.java:739)
                                                                                       at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                       at android.os.Looper.loop(Looper.java:158)
                                                                                       at android.app.ActivityThread.main(ActivityThread.java:7225)
                                                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

0 个答案:

没有答案