android gridview设置gridview适配器导致空指针

时间:2014-07-12 15:34:10

标签: android gridview alertdialog

我有一个Activity来显示自定义gridview中的所有图像,这是我的代码

nutrition_search.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="fu.capstone.hemo.activities.NutritionSearch"
    tools:ignore="MergeRootFrame" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.1"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="20dp"
         android:layout_marginRight="20dp"
        android:orientation="horizontal" >

        <EditText
            android:id="@+id/edtTextSearch"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:hint="Nhập món ăn"
            android:maxLength="20"
            android:maxLines="1" >
        </EditText>

        <Button
            android:id="@+id/imgSearch"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:background="@drawable/ic_search" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.1" 
        >
        <Spinner 
            android:id="@+id/spinDishCategory"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:entries="@array/list_dish_category"
            />
    </LinearLayout>
    <GridView
        android:id="@+id/listDish"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.6"
        android:layout_marginTop="15dp"
        android:layout_marginBottom="15dp"
        android:stretchMode="columnWidth"
        android:numColumns="auto_fit" >
    </GridView>

</LinearLayout>

和我的自定义网格广告:dish_image.xml

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

    <ImageView 
        android:id="@+id/imgDish"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

</LinearLayout>


package fu.capstone.hemo.activities;

import java.util.List;

import fu.capstone.hemo.vo.DishEntity;
import fu.capstone.hemo.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class DishImageAdapter extends BaseAdapter {
    private Context context;
    private List<DishEntity> listDishID;
    LayoutInflater inflater;
    WindowManager windowManager;
    AlertDialog.Builder builder;

public DishImageAdapter(Context context, List<DishEntity> listDishID) {
    // TODO Auto-generated constructor stub
    this.context = context;
    this.listDishID = listDishID;

    windowManager = (WindowManager) context
            .getSystemService(Context.WINDOW_SERVICE);
    inflater = LayoutInflater.from(context);
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return listDishID.size();
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return listDishID.get(position);
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    View view = convertView;
    if (view == null) {
        view = inflater.inflate(R.layout.dish_image, null);
        ImageView imgDish = (ImageView) view.findViewById(R.id.imgDish);
        final int imgDishUrl = context.getResources().getIdentifier(
                listDishID.get(position).getImageUrl(), "drawable",
                context.getPackageName());

        imgDish.requestLayout();
        int size = windowManager.getDefaultDisplay().getWidth()/6;
        imgDish.getLayoutParams().height = size;
        imgDish.getLayoutParams().width = size;
        imgDish.setImageResource(imgDishUrl);

    } else {
        view = (View) convertView;
    }
    return view;
}

}

这是我的SearchPage(在gridview中显示每个的活动)

    package fu.capstone.hemo.activities;

import java.util.List;

import fu.capstone.hemo.R;
import fu.capstone.hemo.dao.DishsDAO;
import fu.capstone.hemo.vo.DishEntity;
import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class NutritionSearch extends ActionBarActivity {
    Spinner spinDishCategory, spinDishChoosen;
    GridView gridDish;
    TextView txtDishName;
    DishsDAO dishDatabase;
    List<DishEntity> dishEntityList;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_nutrition_search);
    dishDatabase = new DishsDAO(this);
    dishDatabase.open();
    dishEntityList = dishDatabase.getAllDishs();
    dishDatabase.close();

    spinDishCategory = (Spinner) findViewById(R.id.spinDishCategory);

    gridDish = (GridView) findViewById(R.id.listDish);
    gridDish.setAdapter(new DishImageAdapter(this, dishEntityList));    
}

没关系,没问题。但是在其他活动中,当我有一个AlertDialog来显示SearchPage,并且当我在setadater时出现空指针问题时,这是我的代码:

private void displayDishSearchPage() {
    LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
    View layout = inflater
            .inflate(R.layout.activity_nutrition_search, null);
    GridView gridDish = (GridView) findViewById(R.id.listDish);
    gridDish.setAdapter(new DishImageAdapter(this, dishEntityList)); <- problem here
    AlertDialog.Builder builder = new AlertDialog.Builder(NutritionDiary.this);
    builder.setView(layout);
    builder.setTitle("Them mon an!");
    builder.setIcon(R.drawable.ic_launcher);
    builder.setNegativeButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface arg0, int arg1) {
            // TODO Auto-generated method stubbuil
            arg0.dismiss();
        }
    });
    builder.show();
}

这是我的logcat

07-12 15:23:53.686: E/AndroidRuntime(1308): FATAL EXCEPTION: main
07-12 15:23:53.686: E/AndroidRuntime(1308): java.lang.NullPointerException
07-12 15:23:53.686: E/AndroidRuntime(1308):     at fu.capstone.hemo.activities.NutritionDiary.displayDishSearchPage(NutritionDiary.java:170)
07-12 15:23:53.686: E/AndroidRuntime(1308):     at fu.capstone.hemo.activities.NutritionDiary.access$0(NutritionDiary.java:165)
07-12 15:23:53.686: E/AndroidRuntime(1308):     at fu.capstone.hemo.activities.NutritionDiary$1.onClick(NutritionDiary.java:1129)
07-12 15:23:53.686: E/AndroidRuntime(1308):     at android.view.View.performClick(View.java:4204)
07-12 15:23:53.686: E/AndroidRuntime(1308):     at android.view.View$PerformClick.run(View.java:17355)
07-12 15:23:53.686: E/AndroidRuntime(1308):     at android.os.Handler.handleCallback(Handler.java:725)
07-12 15:23:53.686: E/AndroidRuntime(1308):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-12 15:23:53.686: E/AndroidRuntime(1308):     at android.os.Looper.loop(Looper.java:137)
07-12 15:23:53.686: E/AndroidRuntime(1308):     at android.app.ActivityThread.main(ActivityThread.java:5041)
07-12 15:23:53.686: E/AndroidRuntime(1308):     at java.lang.reflect.Method.invokeNative(Native Method)
07-12 15:23:53.686: E/AndroidRuntime(1308):     at java.lang.reflect.Method.invoke(Method.java:511)
07-12 15:23:53.686: E/AndroidRuntime(1308):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-12 15:23:53.686: E/AndroidRuntime(1308):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-12 15:23:53.686: E/AndroidRuntime(1308):     at dalvik.system.NativeStart.main(Native Method)

我不知道它是如何导致null指针的,我认为我的ImageAdapter类可以得到任何上下文但它似乎我错了。所以任何人都可以帮助我。非常感谢

1 个答案:

答案 0 :(得分:0)

更改此

 GridView gridDish = (GridView) findViewById(R.id.listDish);

 GridView gridDish = (GridView)layout.findViewById(R.id.listDish);