Nullpointer Exception getter,Android,Fragments,Folding Cell

时间:2017-10-19 09:31:30

标签: java android list exception

我在

上获得了Nullpointer异常
viewHolder.inputvorname.setText(item.getivorname()); 

(代码适配器中的69)。在Android编程中我没有做太多(可能是4到5周)。我尝试了硬编码内容(你会看到)。代码应该以编程方式生成一个新的折叠单元格,内容充满了内容。我希望你能帮助我:

FoldingCellListAdapter.java

import android.app.Fragment;
import android.content.Context;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.RadioButton; 
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;

import com.ramotion.foldingcell.FoldingCell;

import java.util.HashSet;
import java.util.List;
import java.util.zip.Inflater;

import android.app.FragmentManager;
import android.view.LayoutInflater;



public class FoldingCellListAdapter extends ArrayAdapter<Item> {

private HashSet<Integer> unfoldedIndexes = new HashSet<>();


public FoldingCellListAdapter(Context context, List<Item> objects) {
    super(context, 0, objects);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // get item for selected view
    Item item = getItem(position);
    // if cell is exists - reuse it, if not - create the new one from resource
    FoldingCell cell = (FoldingCell) convertView;
    ViewHolder viewHolder;
    if (cell == null) {
        viewHolder = new ViewHolder();
        LayoutInflater vi = LayoutInflater.from(getContext());
        cell = (FoldingCell) vi.inflate(R.layout.foldingcell_pat, parent, 
false);
        // binding view parts to view holder
        viewHolder.inputvorname = (EditText) 
cell.findViewById(R.id.np_inputvorname);
        viewHolder.inputname = (EditText) 
cell.findViewById(R.id.np_inputname);
        viewHolder.inputdate = (EditText) 
cell.findViewById(R.id.np_inputdate);
        viewHolder.inputtime = (EditText) 
cell.findViewById(R.id.np_inputtime);
        viewHolder.inputid = (EditText) cell.findViewById(R.id.np_patID);
        viewHolder.inputage = (EditText) cell.findViewById(R.id.np_patage);
        viewHolder.inputnote = (EditText) 
cell.findViewById(R.id.np_notizen);
        //viewHolder.pledgePrice = (Spinner) 
cell.findViewById(R.id.np_keimespin);
        //viewHolder.pledgePrice = (RadioGroup) 
cell.findViewById(R.id.np_colorgroup);
        cell.setTag(viewHolder);
    } else {
        // for existing cell set valid valid state(without animation)
        if (unfoldedIndexes.contains(position)) {
            cell.unfold(true);
        } else {
            cell.fold(true);
        }
        viewHolder = (ViewHolder) cell.getTag();
    }

    // bind data from selected element to view through view holder
    viewHolder.inputvorname.setText(item.getivorname());
    viewHolder.inputname.setText(item.getiname());
    viewHolder.inputdate.setText(item.getidate());
    viewHolder.inputtime.setText(item.getitime());
    viewHolder.inputid.setText(item.getiid());
    viewHolder.inputage.setText(item.getiage());
    viewHolder.inputnote.setText(item.getinote());




    return cell;
}

// simple methods for register cell state changes
public void registerToggle(int position) {
    if (unfoldedIndexes.contains(position))
        registerFold(position);
    else
        registerUnfold(position);
}

public void registerFold(int position) {
    unfoldedIndexes.remove(position);
}

public void registerUnfold(int position) {
    unfoldedIndexes.add(position);
}


// View lookup cache
private static class ViewHolder {
    EditText inputvorname;
    EditText inputname;
    EditText inputdate;
    EditText inputtime;
    EditText inputid;
    EditText inputage;
    EditText inputnote;
    //Spinner inputkeime;
    //RadioGroup colorgroup;
}

}

Item.java:

import android.app.Fragment;
import android.view.View;

import java.util.ArrayList;


public class Item{
private String ivorname = "Jürgen";
private String iname = "Hans";
private String idate = "1.1.17";
private String itime = "1:1";
private String iid = "P7777";
private int iage = 1;
private String inote = "Hi";
private String ikeime = "MRSA";
private int icolor = 1;

public Item() {
}

public Item (String ivorname, String iname, String idate, String itime, String iid, int iage, String inote, String ikeime, int icolor){
    this.ivorname = ivorname;
    this.iname = iname;
    this.idate = idate;
    this.itime = itime;
    this.iid = iid;
    this.iage = iage;
    this.inote = inote;
    this.ikeime = ikeime;
    this.icolor = icolor;

}

public String getivorname() {return ivorname;}
public String getiname() {return iname;}
public String getidate() {return idate;}
public String getitime() {return itime;}
public String getiid() {return iid;}
public int getiage() {return iage;}
public String getinote() {return inote;}
public String getikeime() {return ikeime;}
public int geticolor() {return icolor;}
public void setivorname(String ivorname) {this.ivorname = ivorname;}
public void setiname(String iname) {this.iname = iname;}
public void setidate(String idate) {this.idate = idate;}
public void setitime(String itime) {this.itime = itime;}
public void setiid(String iid) {this.iid = iid;}
public void setiage(int iage) {this.iage = iage;}
public void setinote(String inote) {this.inote = inote;}
public void setikeime(String ikeime) {this.ikeime = ikeime;}
public void seticolor(int icolor) {this.icolor = icolor;}


@Override
public int hashCode() {
    int result = ivorname != null ? ivorname.hashCode() : 0;
    result = 31 * result + (iname != null ? iname.hashCode() : 0);
    result = 31 * result + (idate != null ? idate.hashCode() : 0);
    result = 31 * result + (itime != null ? itime.hashCode() : 0);
    result = 31 * result + (iid != null ? iid.hashCode() : 0);
    result = 31 * result + (inote != null ? inote.hashCode() : 0);
    result = 31 * result + (ikeime != null ? ikeime.hashCode() : 0);
    return result;
}

public static ArrayList<Item> getTestingList() {
    ArrayList<Item> items = new ArrayList<>();
    items.add(new Item("Peter", "Hans", "19.10.2017", "10:12", "P2546", 45, "Test 123", "MRSA, VRE", 1));
    return items;

}

}

patientsc.java:

public class patientsc extends Fragment {
View myView;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
    getActivity().setTitle("Alle Patienten");
    myView = inflater.inflate(R.layout.patiensl, container, false);

    // get our list view
    ListView theListView = (ListView) myView.findViewById(R.id.patientslist);

    // prepare elements to display
    final ArrayList<Item> items = Item.getTestingList();


    // create custom adapter that holds elements and their state (we need hold a id's of unfolded elements for reusable elements)
    final FoldingCellListAdapter adapter = new FoldingCellListAdapter(getContext(), items);


    // set elements to adapter
    theListView.setAdapter(adapter);

    // set on click event listener to list view
    theListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int pos, long l) {
            // toggle clicked cell state
            ((FoldingCell) view).toggle(false);
            // register in adapter that state for selected cell is toggled
            adapter.registerToggle(pos);
        }
    });

    return myView;
}

}

patiensl.xml:

<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/patientslist"
android:layout_width="match_parent"
android:layout_height="match_parent">

</ListView>

感谢您的帮助。

0 个答案:

没有答案