我正在尝试在gridview中的图像下方添加标题。我尝试了谷歌搜索的不同方法,但失败了。这就是我现在要做的。我试图在一个由另一个片段打开的片段中执行此操作。我收到以下错误:
04-01 22:02:18.331 6726-6726/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.imran.myapp, PID: 6726
java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.TextView
at com.example.imran.myapp.AlbumImagesAdapter.getView(AlbumImagesAdapter.java:64)
at android.widget.AbsListView.obtainView(AbsListView.java:2828)
at android.widget.GridView.makeAndAddView(GridView.java:1437)
at android.widget.GridView.makeRow(GridView.java:361)
at android.widget.GridView.fillDown(GridView.java:302)
at android.widget.GridView.fillFromTop(GridView.java:437)
at android.widget.GridView.layoutChildren(GridView.java:1280)
at android.widget.AbsListView.onLayout(AbsListView.java:2632)
at android.view.View.layout(View.java:16761)
at android.view.ViewGroup.layout(ViewGroup.java:5333)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1702)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1556)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1465)
at android.view.View.layout(View.java:16761)
at android.view.ViewGroup.layout(ViewGroup.java:5333)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1702)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1556)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1465)
at android.view.View.layout(View.java:16761)
at android.view.ViewGroup.layout(ViewGroup.java:5333)
at android.support.design.widget.CoordinatorLayout.layoutChild(CoordinatorLayout.java:1034)
at android.support.design.widget.CoordinatorLayout.onLayoutChild(CoordinatorLayout.java:744)
at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:758)
at android.view.View.layout(View.java:16761)
at android.view.ViewGroup.layout(ViewGroup.java:5333)
at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1043)
at android.view.View.layout(View.java:16761)
at android.view.ViewGroup.layout(ViewGroup.java:5333)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
at android.view.View.layout(View.java:16761)
at android.view.ViewGroup.layout(ViewGroup.java:5333)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1702)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1556)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1465)
at android.view.View.layout(View.java:16761)
at android.view.ViewGroup.layout(ViewGroup.java:5333)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
at android.view.View.layout(View.java:16761)
at android.view.ViewGroup.layout(ViewGroup.java:5333)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1702)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1556)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1465)
at android.view.View.layout(View.java:16761)
at android.view.ViewGroup.layout(ViewGroup.java:5333)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
at android.view.View.layout(View.java:16761)
at android.view.ViewGroup.layout(ViewGroup.java:5333)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2381)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2090)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1239)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6752)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:777)
at android.view.Choreographer.doCallbacks(Choreographer.java:590)
at android.view.Choreographer.doFrame(Choreographer.java:560)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:763)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6145)
at java.lang.reflect
04-01 22:02:18.351 1032-1110/? V/ApplicationPolicy: isApplicationStateBlocked userId 0 pkgname com.example.imran.myapp
04-01 22:02:18.431 1032-1110/? W/ActivityManager: Force finishing activity com.example.imran.myapp/.Home
04-01 22:02:18.431 1032-1110/? V/ApplicationPolicy: isApplicationStateBlocked userId 0 pkgname com.example.imran.myapp
04-01 22:02:18.691 1032-1032/? D/CrashAnrDetector: processName: com.example.imran.myapp
04-01 22:02:18.691 1032-1032/? D/CrashAnrDetector: broadcastEvent : com.example.imran.myapp data_app_crash
04-01 22:02:19.051 1032-1059/? W/ActivityManager: Activity pause timeout for ActivityRecord{1241937f u0 com.example.imran.myapp/.Home t3676 f}
04-01 22:02:29.881 1032-1059/? W/ActivityManager: Activity destroy timeout for ActivityRecord{1241937f u0 com.example.imran.myapp/.Home t3676 f}
MyAdapter
public class AlbumImagesAdapter extends BaseAdapter{
private Context mContext;
public AlbumImagesAdapter(Context c, String urls[]) {
mContext = c;
mThumbIds = urls;
//myurls = urls;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return position;
//return 0;
}
public int imgcount(){
return mThumbIds.length;
}
public String getURL(int position){
return mThumbIds[position];
}
// create a new ImageView for each item referenced by the Adapter
public View getView(final int position, View convertView, ViewGroup parent) {
final ImageView imageView;
final TextView imgtext;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.fragment_openalbum, parent, false);
imgtext = new TextView(mContext);
imageView = new ImageView(mContext);
//imageView.setLayoutParams(new GridView.LayoutParams(250, 250));
imageView.setLayoutParams(new LinearLayout.LayoutParams(300,300));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(12, 12, 12, 12);
} else {
imgtext = (TextView) convertView;
imageView = (ImageView) convertView;
}
imgtext.setText("Some text");
Glide.with(mContext).load(mThumbIds[position]).into(imageView);
return convertView;
}
// references to our images
private String[] mThumbIds = {
};
}
相同类型的stackoverflow问题中描述的大多数方法与我在适配器中使用的方法类似。有人能帮我吗。在此先感谢。
修改:fragment_openalbum.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.imran.myapp.openalbum">
<!-- TODO: Update blank fragment layout -->
<TextView
android:id="@+id/albumname"
android:textSize="25dp"
android:background="@drawable/title_background"
android:layout_gravity="center"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Album Name" />
<TextView
android:id="@+id/albumdesc"
android:background="@drawable/title_background"
android:layout_gravity="center"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:text="Album Description"
/>
<GridView
android:id="@+id/albumgrid"
android:numColumns="auto_fit"
android:columnWidth="120dp"
android:layout_gravity="center"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"></GridView>
</LinearLayout>
答案 0 :(得分:0)
更新: This指南应该给你正确的想法。
所以基本上你不应该在你的适配器R.layout.fragment_openalbum
中夸大getView()
。相反,您应该创建另一个布局来为GridView
的项目提供视图。例如,您可以创建item_gridview.xml
,它看起来就像我给出的两个示例视图中的一个。在getView()
中,您需要R.layout.item_gridview
,然后使用findViewById()
来获取ImageView
和TextView
元素。
您获得的堆栈跟踪实际上指出了错误:
java.lang.ClassCastException:android.widget.LinearLayout无法强制转换为android.widget.TextView
在你的适配器中,你有这个:
imgtext = (TextView) convertView;
imageView = (ImageView) convertView;
你在这里犯了两个错误:
convertView
是android.widget.LinearLayout
,根据堆栈跟踪异常消息,它是ViewGroup
而不是View
。但TextView
或ImageView
是View
的类型。因此,您无法将ViewGroup
类型转换为View
convertView
类型为View
,它也只能是TextView
或 ImageView
。它不可能都是。所以按照你原来的目标:
我正在尝试在网格视图中的图像下方添加标题。
如果要将标题放在图像下方,可以使用此布局:
<LinearLayout ...
android:orientation="vertical">
<ImageView
... />
<TextView
... />
</LinearLayout>
或者,如果您想将标题放在图像上方,但位于图像的底部,则可以使用此布局:
<FrameLayout>
<ImageView
... />
<TextView
...
android:layout_gravity="bottom|center"/>
</FrameLayout>
以下是关于所有不同布局的非常好的介绍,希望您觉得有用
https://medium.com/google-developers/layouts-attributes-and-you-9e5a4b4fe32c#.vh0l7qbvq