2014-02-28 61 views
0

我似乎無法弄清楚這段代碼有什麼問題。我收到的最後一條Log.d消息是「我們在getView中」。之後出現下面的錯誤。我是否正確使用getView,newView和bindView?Gridview/CursorAdapter nullPointerException

錯誤:

02-28 15:22:54.063: E/AndroidRuntime(10855): FATAL EXCEPTION: main 
02-28 15:22:54.063: E/AndroidRuntime(10855): Process: com.example.wwwww_testi, PID: 10855 
02-28 15:22:54.063: E/AndroidRuntime(10855): java.lang.NullPointerException 
02-28 15:22:54.063: E/AndroidRuntime(10855): at android.widget.AbsListView.obtainView(AbsListView.java:2265) 
02-28 15:22:54.063: E/AndroidRuntime(10855): at android.widget.GridView.onMeasure(GridView.java:1044) 
02-28 15:22:54.063: E/AndroidRuntime(10855): at android.view.View.measure(View.java:16497) 
02-28 15:22:54.063: E/AndroidRuntime(10855): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125) 
02-28 15:22:54.063: E/AndroidRuntime(10855): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 
02-28 15:22:54.063: E/AndroidRuntime(10855): at android.view.View.measure(View.java:16497) 
02-28 15:22:54.063: E/AndroidRuntime(10855): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125) 
02-28 15:22:54.063: E/AndroidRuntime(10855): at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327) 
02-28 15:22:54.063: E/AndroidRuntime(10855): at android.view.View.measure(View.java:16497) 
02-28 15:22:54.063: E/AndroidRuntime(10855): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125) 
02-28 15:22:54.063: E/AndroidRuntime(10855): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 
02-28 15:22:54.063: E/AndroidRuntime(10855): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291) 
02-28 15:22:54.063: E/AndroidRuntime(10855): at android.view.View.measure(View.java:16497) 
02-28 15:22:54.063: E/AndroidRuntime(10855): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1916) 
02-28 15:22:54.063: E/AndroidRuntime(10855): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1113) 
02-28 15:22:54.063: E/AndroidRuntime(10855): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1295) 
02-28 15:22:54.063: E/AndroidRuntime(10855): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000) 
02-28 15:22:54.063: E/AndroidRuntime(10855): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670) 
02-28 15:22:54.063: E/AndroidRuntime(10855): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761) 
02-28 15:22:54.063: E/AndroidRuntime(10855): at android.view.Choreographer.doCallbacks(Choreographer.java:574) 
02-28 15:22:54.063: E/AndroidRuntime(10855): at android.view.Choreographer.doFrame(Choreographer.java:544) 
02-28 15:22:54.063: E/AndroidRuntime(10855): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747) 
02-28 15:22:54.063: E/AndroidRuntime(10855): at android.os.Handler.handleCallback(Handler.java:733) 
02-28 15:22:54.063: E/AndroidRuntime(10855): at android.os.Handler.dispatchMessage(Handler.java:95) 
02-28 15:22:54.063: E/AndroidRuntime(10855): at android.os.Looper.loop(Looper.java:136) 
02-28 15:22:54.063: E/AndroidRuntime(10855): at android.app.ActivityThread.main(ActivityThread.java:5017) 
02-28 15:22:54.063: E/AndroidRuntime(10855): at java.lang.reflect.Method.invokeNative(Native Method) 
02-28 15:22:54.063: E/AndroidRuntime(10855): at java.lang.reflect.Method.invoke(Method.java:515) 
02-28 15:22:54.063: E/AndroidRuntime(10855): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
02-28 15:22:54.063: E/AndroidRuntime(10855): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
02-28 15:22:54.063: E/AndroidRuntime(10855): at dalvik.system.NativeStart.main(Native Method) 

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/gridView1" 
    android:numColumns="auto_fit" 
    android:gravity="center" 
    android:columnWidth="50dp" 
    android:stretchMode="columnWidth" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

</GridView> 

grid_item.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/relativelayout" 
    android:layout_width="match_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/header" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_gravity="bottom" 
     android:textColor="#FEFCFF" 
     android:textSize="16sp"/> 

</RelativeLayout> 

Gridadapter.java

public class GridAdapter extends CursorAdapter { 

    private Context mContext; 
    private LayoutInflater mInflater; 
    private final String LOG_TAG = "GridAdapter"; 
    private ViewHolder holder; 

    public GridAdapter(Context context, Cursor c, boolean autoRequery) { 
     super(context, c, autoRequery); 
     this.mContext = context; 
     mInflater = LayoutInflater.from(context); 
     Log.d(LOG_TAG, "We're in constructor"); 
    } 

    public static class ViewHolder { 
     public TextView txtView; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     Log.d(LOG_TAG, "We're in getView"); 
     return null; 
    } 

    @Override 
    public void bindView(View view, Context context, Cursor cursor) { 
     Log.d(LOG_TAG, "We're in bindView"); 
     holder = (ViewHolder) view.getTag(); 
     int headerIndex = cursor.getColumnIndex(ArticleTable.MAIN_HEADER); 
     String header = cursor.getString(headerIndex); 
     holder.txtView.setText(header); 
    } 

    @Override 
    public View newView(Context context, Cursor cursor, ViewGroup parent) { 
     Log.d(LOG_TAG, "We're in newView"); 
     View view = mInflater.inflate(R.layout.grid_item, parent, false); 

     holder = new ViewHolder(); 
     holder.txtView = (TextView) view.findViewById(R.id.header); 
     view.setTag(holder); 
     return view; 
    } 
} 

編輯:這解決了這個問題對我來說:

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    Log.d(LOG_TAG, "We're in getView"); 
    View v; 
    if (convertView == null) { 
     v = newView(mContext, cursor, parent); 
    } else { 
     v = convertView; 
    } 
    bindView(v, mContext, cursor); 
    return v; 
} 
+0

你有'返回null;'在'getView'而它期望一個視圖 – Raghunandan

+0

感謝您的快速幫助! – user2970008

回答

3

getView不能返回null,否則的Android無法衡量它需要借鑑的元素的空間量。您可以避免重寫該方法。

+0

感謝您的回答,我會仔細研究並接受它,因爲我確認它可以解決問題。 – user2970008