java.lang.NullPointerException 与 parent.getChildAt(位置) GridView

java.lang.NullPointerException with parent.getChildAt(position) GridView

当我尝试管理我的 GridView 的项目时遇到一些问题,当位置 if >= 6 时。位置 1,2,3,4,5 一切正常。 这是我的名册 class:

 public class MyRoster extends ActionBarActivity implements AdapterView.OnItemClickListener {

GridView cardsGridView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my_roster);

    cardsGridView = (GridView) findViewById(R.id.cardsGridView);

    cardsGridView.setAdapter(new MyAdapter(this));


    cardsGridView.setOnItemClickListener(this);






}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_my_roster, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}



@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    Log.d("MYTAG", "DENTRO ON ITEMCLICK");

    switch(parent.getId()){

        case R.id.cardsGridView:

            ImageView prova = (ImageView)parent.getChildAt(position+1);
           // ImageView prova = (ImageView)parent.getItemAtPosition(position+1);

            prova.setImageResource(R.drawable.checked_checkbox);



            break;


    }

}
}

这是用于管理 GridView 项目的 class 适配器:

public class MyAdapter  extends BaseAdapter{

private List<Item> items = new ArrayList<Item>();
private LayoutInflater inflater;

Integer[] myCardsInteger = takeMyCardsFromDB(); // return an int array

public MyAdapter(Context context) {
    inflater = LayoutInflater.from(context);

    for( int i = 0; i < myCardsInteger.length; i++){
        String key = "" + i;
        items.add(new Item(key, myCardsInteger[i] ));
        items.add(new Item(key, R.drawable.unchecked_checkbox));
    }

}


// da implementare!!
private Integer[] takeMyCardsFromDB(){

    // prendo le carte che possiede l'utente dal database interno
    Integer[] prova = {R.drawable.two, R.drawable.one, R.drawable.three, R.drawable.four,R.drawable.five,R.drawable.ahah,R.drawable.foeuehu};
    return prova;
}

@Override
public int getCount() {
    return items.size();
}

@Override
public Object getItem(int i) {
    return items.get(i);
}

@Override
public long getItemId(int i) {
    return items.get(i).drawableId;
}

@Override
public View getView(int i, View view, ViewGroup viewGroup) {
    View v = view;
    ImageView picture;
    TextView name;

    if(v == null) {
        v = inflater.inflate(R.layout.row_my_roster_card, viewGroup, false);
        v.setTag(R.id.picture, v.findViewById(R.id.picture));

    }

    picture = (ImageView)v.getTag(R.id.picture);

    Item item = (Item)getItem(i);

    picture.setImageResource(item.drawableId);


    return v;
}

private class Item {
    final String name;
    final int drawableId;

    Item(String name, int drawableId) {
        this.name = name;
        this.drawableId = drawableId;
    }
}

}

GridView单项为ImageView。 布局代码:

 <RelativeLayout 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.oldmount.fba.MyRoster">

<LinearLayout
    android:id="@+id/prova"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:text="SELEZIONA LA TUA ROSA!"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:layout_below="@id/prova">

    <GridView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/cardsGridView"
        android:numColumns="2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>




</LinearLayout>

当我点击一个项目时,我想在下一个位置更改该项目。 在我的适配器 class 中,我实例化了超过 12 个项目, 但是当我点击项目编号 6 和后续项目时,我有一个 nullpointerexception。

logcat:

 29 19:22:38.867    2420-2420/-------E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: --------, PID: 2420
java.lang.NullPointerException
        at -----.MyRoster.onItemClick(MyRoster.java:82)
        at android.widget.AdapterView.performItemClick(AdapterView.java:308)
        at android.widget.AbsListView.performItemClick(AbsListView.java:1510)
        at android.widget.AbsListView$PerformClick.run(AbsListView.java:3472)
        at android.widget.AbsListView.run(AbsListView.java:4835)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:146)
        at android.app.ActivityThread.main(ActivityThread.java:5593)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
        at dalvik.system.NativeStart.main(Native Method)

这个错误让我失去了理智。有人能帮我吗?

我认为这段代码是你的问题。

ImageView prova = (ImageView)parent.getChildAt(position+1);

在您的 MyAdapter class 中添加此方法:

 public void setView(int position ){

    String key = ""+ position;
    items.remove(position);
    items.add(position , new Item(key, R.drawable.checked_checkbox));

}

在您的 MyRoster class 中实例化一个 MyAdapter 对象并在您的 OnItemClick 中使用此代码:

 myAdapter.setView(position);

您总是直接在适配器中更改项目class以保留更改。