Football Fans: Download the 2012 Schedule App from Google Play!


Go Back   Android Forums > Android Development > Application Development

Application Development Dev Lounge for the Coder Folks



Reply
 
LinkBack Thread Tools
Old January 26th, 2012, 09:29 AM   #1 (permalink)
New Member
 
Join Date: May 2011
Posts: 6
 
Device(s):
Thanks: 0
Thanked 0 Times in 0 Posts
Default GridView application keeps crashing

I am new to android, my application keeps crashing with the following errors. I am using API Level 15.

01-26 19:50:45.820: E/AndroidRuntime(963): FATAL EXCEPTION: main
01-26 19:50:45.820: E/AndroidRuntime(963): java.lang.NullPointerException
01-26 19:50:45.820: E/AndroidRuntime(963): at my.grid.view.DataAdapter.getView(DataAdapter.java: 62)
01-26 19:50:45.820: E/AndroidRuntime(963): at android.widget.AbsListView.obtainView(AbsListView. java:2012)
01-26 19:50:45.820: E/AndroidRuntime(963): at android.widget.GridView.makeAndAddView(GridView.ja va:1323)
01-26 19:50:45.820: E/AndroidRuntime(963): at android.widget.GridView.makeRow(GridView.java:328)
01-26 19:50:45.820: E/AndroidRuntime(963): at android.widget.GridView.moveSelection(GridView.jav a:885)
01-26 19:50:45.820: E/AndroidRuntime(963): at android.widget.GridView.layoutChildren(GridView.ja va:1230)
01-26 19:50:45.820: E/AndroidRuntime(963): at android.widget.GridView.setSelectionInt(GridView.j ava:1476)
01-26 19:50:45.820: E/AndroidRuntime(963): at android.widget.GridView.arrowScroll(GridView.java: 1729)
01-26 19:50:45.820: E/AndroidRuntime(963): at android.widget.GridView.commonKey(GridView.java:15 43)
01-26 19:50:45.820: E/AndroidRuntime(963): at android.widget.GridView.onKeyDown(GridView.java:14 94)
01-26 19:50:45.820: E/AndroidRuntime(963): at android.view.KeyEvent.dispatch(KeyEvent.java:2551)
01-26 19:50:45.820: E/AndroidRuntime(963): at android.view.View.dispatchKeyEvent(View.java:5500)
01-26 19:50:45.820: E/AndroidRuntime(963): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup. java:1242)
01-26 19:50:45.820: E/AndroidRuntime(963): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup. java:1246)
01-26 19:50:45.820: E/AndroidRuntime(963): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup. java:1246)
01-26 19:50:45.820: E/AndroidRuntime(963): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup. java:1246)
01-26 19:50:45.820: E/AndroidRuntime(963): at com.android.internal.policy.impl.PhoneWindow$Decor View.superDispatchKeyEvent(PhoneWindow.java:1879)
01-26 19:50:45.820: E/AndroidRuntime(963): at com.android.internal.policy.impl.PhoneWindow.super DispatchKeyEvent(PhoneWindow.java:1361)
01-26 19:50:45.820: E/AndroidRuntime(963): at android.app.Activity.dispatchKeyEvent(Activity.jav a:2324)
01-26 19:50:45.820: E/AndroidRuntime(963): at com.android.internal.policy.impl.PhoneWindow$Decor View.dispatchKeyEvent(PhoneWindow.java:1806)
01-26 19:50:45.820: E/AndroidRuntime(963): at android.view.ViewRootImpl.deliverKeyEventPostIme(V iewRootImpl.java:3327)
01-26 19:50:45.820: E/AndroidRuntime(963): at android.view.ViewRootImpl.handleFinishedEvent(View RootImpl.java:3300)
01-26 19:50:45.820: E/AndroidRuntime(963): at android.view.ViewRootImpl.handleMessage(ViewRootIm pl.java:2460)
01-26 19:50:45.820: E/AndroidRuntime(963): at android.os.Handler.dispatchMessage(Handler.java:99 )
01-26 19:50:45.820: E/AndroidRuntime(963): at android.os.Looper.loop(Looper.java:137)
01-26 19:50:45.820: E/AndroidRuntime(963): at android.app.ActivityThread.main(ActivityThread.jav a:4424)
01-26 19:50:45.820: E/AndroidRuntime(963): at java.lang.reflect.Method.invokeNative(Native Method)
01-26 19:50:45.820: E/AndroidRuntime(963): at java.lang.reflect.Method.invoke(Method.java:511)
01-26 19:50:45.820: E/AndroidRuntime(963): at com.android.internal.os.ZygoteInit$MethodAndArgsCa ller.run(ZygoteInit.java:784)
01-26 19:50:45.820: E/AndroidRuntime(963): at com.android.internal.os.ZygoteInit.main(ZygoteInit .java:551)
01-26 19:50:45.820: E/AndroidRuntime(963): at dalvik.system.NativeStart.main(Native Method)
main.xml
--------
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:stretchMode="columnWidth"
android:cacheColorHint="#00000000"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="1"
android:clipChildren="true"
android:horizontalSpacing="3dip"
android:verticalSpacing="3dip" />


customgrid.xml
--------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
androidrientation="vertical">
<TableLayout
android:id="@+id/TableLayout01"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
<TableRow
android:id="@+id/TableRow01"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
<TextView
android:text="@string/hello"
android:layout_width="100dp"
android:layout_height="15dp"
android:textSize="12dp"
android:id="@+id/txtName" />
<TextView
android:text="@string/hello"
android:layout_width="35dp"
android:layout_height="15dp"
android:textSize="12dp"
android:id="@+id/txtId" />
<TextView
android:text="@string/hello"
android:layout_width="70dp"
android:layout_height="15dp"
android:textSize="12dp"
android:id="@+id/txtRate" />
</TableRow>
</TableLayout>
</LinearLayout>


GridViewDataActivity.java
-------------------------
import android.app.Activity;
import android.os.Bundle;
import android.widget.GridView;

public class GridViewDataActivity extends Activity
{
@Override
public void onCreate( Bundle savedInstanceState )
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new DataAdapter(this));
}
}


DataAdapter.java
----------------
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class DataAdapter extends BaseAdapter
{
Context mContext;
private String[] name = { "Rohit", "Rahul", "Ravi", "Amit", "Arun", "Anil",
"Kashif", "Rohit1", "Rahul1", "Ravi1", "Amit1",
"Arun1", "Anil1", "Kashif1", "Rohit2", "Rahul2",
"Ravi2", "Amit2", "Arun2", "Anil2", "Kashif2" };
private String[] id = { "S001", "S002", "S003", "S004", "S005", "S006", "S007", "S0011", "S0021", "S0031", "S0041",
"S0051", "S0061", "S0071", "S0012", "S0022", "S0032", "S0042", "S0052", "S0062", "S0072" };
private String[] rate = { "2500.00", "3500.00", "1000.00", "500.00", "9000.00", "10000.00", "5000.00", "12500.00",
"13500.00", "11000.00", "1500.00", "19000.00", "110000.00", "15000.00", "22500.00", "23500.00",
"21000.00", "2500.00",
"29000.00", "210000.00", "25000.00" };
private LayoutInflater mInflater;

public DataAdapter( Context c )
{
mContext = c;
mInflater = LayoutInflater.from(c);
}

public int getCount()
{
return id.length;
}

public Object getItem( int position )
{
return position;
}

public long getItemId( int position )
{
return position;
}

public View getView( int position, View convertView, ViewGroup parent )
{
ViewHolder holder = null;
if (convertView == null)
{
convertView = mInflater.inflate(R.layout.customgrid, parent, false);
holder = new ViewHolder();
holder.txtName = (TextView) convertView.findViewById(R.id.txtName);
holder.txtName.setGravity(Gravity.LEFT);
holder.txtId = (TextView) convertView.findViewById(R.id.txtId);
holder.txtId.setGravity(Gravity.LEFT);
holder.txtRate = (TextView) convertView.findViewById(R.id.txtRate);
holder.txtRate.setGravity(Gravity.RIGHT);
if (position == 0)
{
convertView.setTag(holder);
}
}
else
{
holder = (ViewHolder) convertView.getTag();
}
/******************************************/
/*** This line is giving the problem *****/
/******************************************/
holder.txtName.setText(name[position]);
holder.txtId.setText(id[position]);
holder.txtRate.setText(rate[position]);
return convertView;
}

static class ViewHolder
{
TextView txtName;
TextView txtId;
TextView txtRate;
}
}

Any idea what is causing the problem?

sudheer is offline  
Reply With Quote
Sponsors
Old January 26th, 2012, 11:13 AM   #2 (permalink)
Junior Member
 
jonathanrz's Avatar
 
Join Date: Jan 2012
Location: Brazil - Blumenau, SC
Posts: 69
 
Device(s): Motorola Defy - Gingerbread(2.3.3)
Thanks: 12
Thanked 5 Times in 4 Posts
Send a message via MSN to jonathanrz jonathanrafaelz
Default

Quote:
Originally Posted by sudheer View Post
01-26 19:50:45.820: E/AndroidRuntime(963): java.lang.NullPointerException
01-26 19:50:45.820: E/AndroidRuntime(963): at my.grid.view.DataAdapter.getView(DataAdapter.java: 62)
The first line "java.lang.NullPointerException" indicates that a null pointer was used.

The second line "my.grid.view.DataAdapter.getView" indicates the last method called.

We know that getView must return the view to the Grid, I believe you returned null.

Here is the google tutorial to the GridView: Grid View | Android Developers

If you can't find the bug, post the code of the method getView from the class DataAdapter.
jonathanrz is offline  
Reply With Quote
Old January 26th, 2012, 12:02 PM   #3 (permalink)
New Member
 
Join Date: May 2011
Posts: 6
 
Device(s):
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by jonathanrz View Post
The first line "java.lang.NullPointerException" indicates that a null pointer was used.

The second line "my.grid.view.DataAdapter.getView" indicates the last method called.

We know that getView must return the view to the Grid, I believe you returned null.

Here is the google tutorial to the GridView: Grid View | Android Developers

If you can't find the bug, post the code of the method getView from the class DataAdapter.


Hi,

Thanks for your reply. I have already posted the code of the method getView. It is under DataAdapter.java.

Regards.

Sudheer
sudheer is offline  
Reply With Quote
Old January 26th, 2012, 01:04 PM   #4 (permalink)
Junior Member
 
jonathanrz's Avatar
 
Join Date: Jan 2012
Location: Brazil - Blumenau, SC
Posts: 69
 
Device(s): Motorola Defy - Gingerbread(2.3.3)
Thanks: 12
Thanked 5 Times in 4 Posts
Send a message via MSN to jonathanrz jonathanrafaelz
Default

Why you are just setting the tag if position is 0?

I believe that android is calling the getView for all itens, and you are setting the tag just in the first item, when android calls again getView for position 1, you try to get the tag from convertView, but you didn't called setTag for convertView 1, then getTag returns a null object, and you try to get the object txtName from a null object, and then the error occurs.
jonathanrz is offline  
Reply With Quote
Old January 26th, 2012, 02:38 PM   #5 (permalink)
Senior Member
 
Join Date: Jul 2010
Posts: 977
 
Device(s): Samsung Galaxy S II, HTC Evo 4G, Amazon Kindle Fire
Thanks: 52
Thanked 199 Times in 144 Posts
Default

What is your line 62?
jonbonazza is online now  
Reply With Quote
Old January 26th, 2012, 07:54 PM   #6 (permalink)
New Member
 
Join Date: May 2011
Posts: 6
 
Device(s):
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by jonbonazza View Post
What is your line 62?
Yes, it is line 62, giving the error.
sudheer is offline  
Reply With Quote
Old January 26th, 2012, 07:59 PM   #7 (permalink)
New Member
 
Join Date: May 2011
Posts: 6
 
Device(s):
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by jonathanrz View Post
Why you are just setting the tag if position is 0?

I believe that android is calling the getView for all itens, and you are setting the tag just in the first item, when android calls again getView for position 1, you try to get the tag from convertView, but you didn't called setTag for convertView 1, then getTag returns a null object, and you try to get the object txtName from a null object, and then the error occurs.
Actually it is based on this example.

Display Data in GridView in Android - MindStick

The error shows only when the list starts scrolling and the highlight bar is moved up or down. If there are only few rows, i.e., scrolling is not required it works perfectly.
sudheer is offline  
Reply With Quote
Old January 27th, 2012, 12:20 AM   #8 (permalink)
Over Macho Grande?
 
alostpacket's Avatar
 
Join Date: Nov 2009
Location: NY
Posts: 7,090
 
Device(s): GalaxyNexus(LTE), NexusOne, OG Droid, GalaxyTab 10.1(LTE), Eris, Logitech Revue (fishtank)
Thanks: 4,164
Thanked 3,126 Times in 1,292 Posts
Default

Quote:
Originally Posted by sudheer View Post
Yes, it is line 62, giving the error.

He means, what exactly is your code on that line (since we dont have your code with line numbers here on the forums)

When you look at line 62, it should be pretty clear what is null.

For example is you call a method on an object:

Code:
someObject.doSomething();
If code like this was on line 62, you would know that the problem is someObject is null


hth
alostpacket is offline  
Reply With Quote
Old January 27th, 2012, 06:32 AM   #9 (permalink)
Junior Member
 
jonathanrz's Avatar
 
Join Date: Jan 2012
Location: Brazil - Blumenau, SC
Posts: 69
 
Device(s): Motorola Defy - Gingerbread(2.3.3)
Thanks: 12
Thanked 5 Times in 4 Posts
Send a message via MSN to jonathanrz jonathanrafaelz
Default

Quote:
Originally Posted by sudheer View Post
Actually it is based on this example.

Display Data in GridView in Android - MindStick

The error shows only when the list starts scrolling and the highlight bar is moved up or down. If there are only few rows, i.e., scrolling is not required it works perfectly.
In the example, have just 7 lines, in your code, have 21.

So I believe that scrolling will not work in the example.

Try removing the "if (position == 0)".
jonathanrz is offline  
Reply With Quote
Old January 27th, 2012, 07:14 PM   #10 (permalink)
New Member
 
Join Date: May 2011
Posts: 6
 
Device(s):
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by jonathanrz View Post
In the example, have just 7 lines, in your code, have 21.

So I believe that scrolling will not work in the example.

Try removing the "if (position == 0)".

That worked! Thank you.
sudheer is offline  
Reply With Quote
Sponsors
Reply

Bookmarks


Go Back   Android Forums > Android Development > Application Development User CP
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -5. The time now is 12:10 PM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
Custom vBulletin Skins by: Relivo