网格视图下的文本视图

时间:2014-03-04 10:56:51

标签: android gridview crash textview

如何在网格视图中的图像下添加文本视图? 我得到了这个代码,但是当我启动应用程序时它会崩溃。

public class ImageAdapter extends BaseAdapter {
    private Context mContext;
    TextView text;
    private String[] mThumbTxt = {
            "Some text view 1", "Some text view 2", "Some text view 3", " Some text view 4"
    };


    public Integer[] mThumbIds = {
            R.drawable.img1, R.drawable.img2,
        R.drawable.img3,  R.drawable.img4, 
        R.drawable.img5,  R.drawable.img6, 
        R.drawable.img7,  R.drawable.img20, 
        R.drawable.img8,  R.drawable.img21, 
        R.drawable.img9,  R.drawable.img22, 
        R.drawable.img10,  R.drawable.img23, 
        R.drawable.img11,  R.drawable.img24, 
        R.drawable.img12,  R.drawable.img25, 
        R.drawable.img13,  R.drawable.img26, 
        R.drawable.img14,  R.drawable.img27, 
        R.drawable.img15,  R.drawable.img28, 
        R.drawable.img16,  R.drawable.img29, 
        R.drawable.img17,  R.drawable.img30, 
        R.drawable.img18,  R.drawable.img31, 
        R.drawable.img19,  
};

// Constructor
public ImageAdapter(Context c){
    mContext = c;
}

@Override
public int getCount() {
    return mThumbIds.length;
}

@Override
public Object getItem(int position) {
    return mThumbIds[position];
}

@Override
public long getItemId(int position) {
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView = new ImageView(mContext);
    imageView.setImageResource(mThumbIds[position]);
    imageView.setScaleType(ImageView.ScaleType.FIT_XY);
    imageView.setLayoutParams(new GridView.LayoutParams(270, 270));
    text.setText(mThumbTxt[position]);
    return imageView;
}


}

那我该怎么办?在网格视图中创建文本视图?或者我可以用字符串做到吗? 使用此代码我得到错误。

03-04 11:53:50.188: W/dalvikvm(12926): threadid=1: thread exiting with uncaught exception (group=0x41aac8b0)
03-04 11:53:50.208: E/AndroidRuntime(12926): FATAL EXCEPTION: main
03-04 11:53:50.208: E/AndroidRuntime(12926): java.lang.NullPointerException
03-04 11:53:50.208: E/AndroidRuntime(12926):    at gsoft.relaxingringtones.ImageAdapter.getView(ImageAdapter.java:64)
03-04 11:53:50.208: E/AndroidRuntime(12926):    at android.widget.AbsListView.obtainView(AbsListView.java:2181)
03-04 11:53:50.208: E/AndroidRuntime(12926):    at android.widget.GridView.onMeasure(GridView.java:1044)
03-04 11:53:50.208: E/AndroidRuntime(12926):    at android.view.View.measure(View.java:16030)
03-04 11:53:50.208: E/AndroidRuntime(12926):    at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:728)
03-04 11:53:50.208: E/AndroidRuntime(12926):    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:477)
03-04 11:53:50.208: E/AndroidRuntime(12926):    at android.view.View.measure(View.java:16030)
03-04 11:53:50.208: E/AndroidRuntime(12926):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
03-04 11:53:50.208: E/AndroidRuntime(12926):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:314)
03-04 11:53:50.208: E/AndroidRuntime(12926):    at android.view.View.measure(View.java:16030)
03-04 11:53:50.208: E/AndroidRuntime(12926):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
03-04 11:53:50.208: E/AndroidRuntime(12926):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
03-04 11:53:50.208: E/AndroidRuntime(12926):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
03-04 11:53:50.208: E/AndroidRuntime(12926):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
03-04 11:53:50.208: E/AndroidRuntime(12926):    at android.view.View.measure(View.java:16030)
03-04 11:53:50.208: E/AndroidRuntime(12926):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
03-04 11:53:50.208: E/AndroidRuntime(12926):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:314)
03-04 11:53:50.208: E/AndroidRuntime(12926):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2196)
03-04 11:53:50.208: E/AndroidRuntime(12926):    at android.view.View.measure(View.java:16030)
03-04 11:53:50.208: E/AndroidRuntime(12926):    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1915)
03-04 11:53:50.208: E/AndroidRuntime(12926):    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1109)
03-04 11:53:50.208: E/AndroidRuntime(12926):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1290)
03-04 11:53:50.208: E/AndroidRuntime(12926):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1009)
03-04 11:53:50.208: E/AndroidRuntime(12926):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5508)
03-04 11:53:50.208: E/AndroidRuntime(12926):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
03-04 11:53:50.208: E/AndroidRuntime(12926):    at android.view.Choreographer.doCallbacks(Choreographer.java:562)
03-04 11:53:50.208: E/AndroidRuntime(12926):    at android.view.Choreographer.doFrame(Choreographer.java:532)
03-04 11:53:50.208: E/AndroidRuntime(12926):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
03-04 11:53:50.208: E/AndroidRuntime(12926):    at android.os.Handler.handleCallback(Handler.java:730)
03-04 11:53:50.208: E/AndroidRuntime(12926):    at android.os.Handler.dispatchMessage(Handler.java:92)
03-04 11:53:50.208: E/AndroidRuntime(12926):    at android.os.Looper.loop(Looper.java:213)
03-04 11:53:50.208: E/AndroidRuntime(12926):    at android.app.ActivityThread.main(ActivityThread.java:5225)
03-04 11:53:50.208: E/AndroidRuntime(12926):    at java.lang.reflect.Method.invokeNative(Native Method)
03-04 11:53:50.208: E/AndroidRuntime(12926):    at java.lang.reflect.Method.invoke(Method.java:525)
03-04 11:53:50.208: E/AndroidRuntime(12926):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741)
03-04 11:53:50.208: E/AndroidRuntime(12926):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
03-04 11:53:50.208: E/AndroidRuntime(12926):    at dalvik.system.NativeStart.main(Native Method)

3 个答案:

答案 0 :(得分:3)

","

的最后一个位置删除逗号mThumbIds

改变它

 public Integer[] mThumbIds = {
            R.drawable.img1, R.drawable.img2,
      ....................................
        R.drawable.img18,  R.drawable.img31, 
        R.drawable.img19,  <---- remove comma from here.
};

按如下所示编写数组:

 public Integer[] mThumbIds = {
            R.drawable.img1, R.drawable.img2,
      ....................................
        R.drawable.img18,  R.drawable.img31, 
        R.drawable.img19  <---- removed comma from here.
};

此外,您忘记在TextView方法中初始化getView,这就是为TextView投放nullpointer异常的原因。

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView = new ImageView(mContext);
     text=new TextView(mContext);
  ..........................................
    text.setText(mThumbTxt[position]);
    return imageView;
}

始终确保mThumbTxt的大小必须与mThumbIds相同,否则会抛出IndexOutOfBounds的错误。

答案 1 :(得分:1)

我认为你应该在使用

之前在getView方法中初始化textview
text = new TextView(this);
text.setText(mThumbTxt[position]);

添加textview后,您必须返回类型为linearlayout或RelativeLayout的视图才能显示图像和文本。

答案 2 :(得分:1)

您需要为findViewById添加TextView或动态添加TextView ..

text.setText(mThumbTxt[position]);  

代码不知道在哪里设置该文字,“text”是