GridView onItemClickListener NullPointerException

时间:2014-04-29 19:52:25

标签: android gridview dialog nullpointerexception

我正在使用gridI,onItemClickListener打开一个对话框,但是我在对话框中得到了一个N​​PE,我无法弄清楚如何解决它。非常感谢!

这是我的Galerie.java:

package com.descoper.rom;

import java.util.ArrayList;


import android.os.Bundle;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ListAdapter;
import android.widget.TextView;
import android.widget.Toast;



public class Galerie extends Activity {
    GridView gridView;
    ArrayList<Item> gridArray = new ArrayList<Item>();
     GalerieAdapter customGridAdapter;
     private Dialog dialog;


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.galerie);

        //set grid view item
        Bitmap homeIcon = BitmapFactory.decodeResource(this.getResources(), R.drawable.ro);
        Bitmap userIcon = BitmapFactory.decodeResource(this.getResources(), R.drawable.ro);

        gridArray.add(new Item(homeIcon,"Home"));
        gridArray.add(new Item(userIcon,"User"));
        gridArray.add(new Item(homeIcon,"House"));
        gridArray.add(new Item(userIcon,"Friend"));
        gridArray.add(new Item(homeIcon,"Home"));
        gridArray.add(new Item(userIcon,"Personal"));
        gridArray.add(new Item(homeIcon,"Home"));
        gridArray.add(new Item(userIcon,"User"));
        gridArray.add(new Item(homeIcon,"Building"));
        gridArray.add(new Item(userIcon,"User"));
        gridArray.add(new Item(homeIcon,"Home"));
        gridArray.add(new Item(userIcon,"xyz"));


        gridView = (GridView) findViewById(R.id.gridView1);
        customGridAdapter = new GalerieAdapter(this, R.layout.galerie_row, gridArray);
        gridView.setAdapter((ListAdapter) customGridAdapter);
        gridView.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if(position == 0){
                    dialog = new Dialog(getApplicationContext());
                    dialog.setContentView(R.layout.dialog1);
                    dialog.setTitle(position);

                    TextView text = (TextView) dialog.findViewById(R.id.textView1);
                    Button btnSave = (Button) dialog.findViewById(R.id.button1);

                    btnSave.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {

                            dialog.dismiss();

                        }
                    });
                    dialog.show();
                }


               if(position == 1){
                   Toast.makeText(Galerie.this, "APASATAAAAAAA" + position, Toast.LENGTH_SHORT).show();
               }
               if(position ==2){
                   Toast.makeText(Galerie.this, "APASATAAAAAAA" + position, Toast.LENGTH_SHORT).show();
               }
            }
        });

    }
}

dialog1.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" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>

logcat的:

04-29 15:38:59.614: E/AndroidRuntime(1272): FATAL EXCEPTION: main
04-29 15:38:59.614: E/AndroidRuntime(1272): Process: com.descoper.rom, PID: 1272
04-29 15:38:59.614: E/AndroidRuntime(1272): java.lang.NullPointerException
04-29 15:38:59.614: E/AndroidRuntime(1272):     at android.app.Dialog.<init>(Dialog.java:156)
04-29 15:38:59.614: E/AndroidRuntime(1272):     at android.app.Dialog.<init>(Dialog.java:133)
04-29 15:38:59.614: E/AndroidRuntime(1272):     at com.descoper.rom.Galerie$1.onItemClick(Galerie.java:61)
04-29 15:38:59.614: E/AndroidRuntime(1272):     at android.widget.AdapterView.performItemClick(AdapterView.java:299)
04-29 15:38:59.614: E/AndroidRuntime(1272):     at android.widget.AbsListView.performItemClick(AbsListView.java:1113)
04-29 15:38:59.614: E/AndroidRuntime(1272):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:2904)
04-29 15:38:59.614: E/AndroidRuntime(1272):     at android.widget.AbsListView$3.run(AbsListView.java:3638)
04-29 15:38:59.614: E/AndroidRuntime(1272):     at android.os.Handler.handleCallback(Handler.java:733)
04-29 15:38:59.614: E/AndroidRuntime(1272):     at android.os.Handler.dispatchMessage(Handler.java:95)
04-29 15:38:59.614: E/AndroidRuntime(1272):     at android.os.Looper.loop(Looper.java:136)
04-29 15:38:59.614: E/AndroidRuntime(1272):     at android.app.ActivityThread.main(ActivityThread.java:5017)
04-29 15:38:59.614: E/AndroidRuntime(1272):     at java.lang.reflect.Method.invokeNative(Native Method)
04-29 15:38:59.614: E/AndroidRuntime(1272):     at java.lang.reflect.Method.invoke(Method.java:515)
04-29 15:38:59.614: E/AndroidRuntime(1272):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-29 15:38:59.614: E/AndroidRuntime(1272):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-29 15:38:59.614: E/AndroidRuntime(1272):     at dalvik.system.NativeStart.main(Native Method)

Galerie.java第61行:

dialog = new Dialog(getApplicationContext());

2 个答案:

答案 0 :(得分:2)

您应该使用Activity上下文来显示对话框,而不是getApplicationContext

更改

    dialog = new Dialog(getApplicationContext());

    dialog = new Dialog(Galerie.this); 

当您使用上下文来修改UI时,您应该使用Activity上下文,因为对话框由Activity拥有。即Galerie.this

当您使用上下文执行非活动时,您应该使用应用程序上下文。

答案 1 :(得分:0)

尝试移动

dialog = new Dialog(getApplicationContext());
dialog.setContentView(R.layout.dialog1);

onItemClick之外。

另外,使用 dialog = new Dialog(this); 代替dialog = new Dialog(getApplicationContext())