Reputation: 389
I have to create an AlertDialog for the user choose a category in the gridview, however I'm getting error. This is my code. When I click in the TextView to call the method that work in show dialog, the app crash. Anyone know some tutorial?
category_grid.xml
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/category_grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="90dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="2"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp" >
</GridView>
grid_single.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="wrap_content"
android:orientation="vertical"
android:background="@color/vermelho_row" >
<ImageView
android:id="@+id/category_image"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/ic_action_image_photo_camera"
android:layout_gravity="center"/>
<TextView
android:id="@+id/category_name"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:gravity="center"
android:text="Experimentar"
android:background="@color/laranja_row"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"/>
</LinearLayout>
BaseAdapter
package com.fb.newcomersapp;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class CustomGrid extends BaseAdapter{
private Context context;
private String [] categoryName;
private int [] categoryImage;
public CustomGrid(Context context, String [] categoryName, int [] categoryImage){
this.context = context;
this.categoryName = categoryName;
this.categoryImage = categoryImage;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 0;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View grid;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(convertView == null){
grid = new View(context);
grid = inflater.inflate(R.layout.grid_single, null);
TextView textView = (TextView) grid.findViewById(R.id.category_name);
ImageView imageView = (ImageView) grid.findViewById(R.id.category_image);
textView.setText(categoryName[position]);
imageView.setImageResource(categoryImage[position]);
}
else{
grid = (View) convertView;
}
return grid;
}
}
Method in Activity to show dialog
private void showAlertDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
CustomGrid adapter = new CustomGrid(NewEventActivity.this, categoryName, categoryImage);
GridView grid = (GridView) findViewById(R.id.category_grid);
grid.setAdapter(adapter);
builder.setView(grid);
builder.setTitle("Goto");
builder.show();
}
** Log **
01-04 23:44:38.727: E/AndroidRuntime(10444): FATAL EXCEPTION: main
01-04 23:44:38.727: E/AndroidRuntime(10444): Process: com.fb.newcomersapp, PID: 10444
01-04 23:44:38.727: E/AndroidRuntime(10444): java.lang.NullPointerException
01-04 23:44:38.727: E/AndroidRuntime(10444): at com.fb.newcomersapp.NewEventActivity.showAlertDialog(NewEventActivity.java:205)
01-04 23:44:38.727: E/AndroidRuntime(10444): at com.fb.newcomersapp.NewEventActivity.access$6(NewEventActivity.java:199)
01-04 23:44:38.727: E/AndroidRuntime(10444): at com.fb.newcomersapp.NewEventActivity$8.onClick(NewEventActivity.java:190)
01-04 23:44:38.727: E/AndroidRuntime(10444): at android.view.View.performClick(View.java:4456)
01-04 23:44:38.727: E/AndroidRuntime(10444): at android.view.View$PerformClick.run(View.java:18465)
01-04 23:44:38.727: E/AndroidRuntime(10444): at android.os.Handler.handleCallback(Handler.java:733)
01-04 23:44:38.727: E/AndroidRuntime(10444): at android.os.Handler.dispatchMessage(Handler.java:95)
01-04 23:44:38.727: E/AndroidRuntime(10444): at android.os.Looper.loop(Looper.java:136)
01-04 23:44:38.727: E/AndroidRuntime(10444): at android.app.ActivityThread.main(ActivityThread.java:5086)
01-04 23:44:38.727: E/AndroidRuntime(10444): at java.lang.reflect.Method.invokeNative(Native Method)
01-04 23:44:38.727: E/AndroidRuntime(10444): at java.lang.reflect.Method.invoke(Method.java:515)
01-04 23:44:38.727: E/AndroidRuntime(10444): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
01-04 23:44:38.727: E/AndroidRuntime(10444): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
01-04 23:44:38.727: E/AndroidRuntime(10444): at dalvik.system.NativeStart.main(Native Method)
Upvotes: 0
Views: 62
Reputation: 271
If you post your logcat it'll be easier to help you.
But I am guessing you are getting a NullPointerException on the line
grid.setAdapter(adapter);
When you call
GridView grid = (GridView) findViewById(R.id.category_grid);
You are going to get null (Unless their is a category_grid in your current layout) I think instead what you want to do is inflate the grid layout.
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
GridView grid = inflater.inflate(R.layout.category_grid, null);
Upvotes: 1