【问题标题】:App crashes when using GridView inside fragment在片段内使用 GridView 时应用程序崩溃
【发布时间】:2016-08-05 14:24:44
【问题描述】:

当 textView 更改为 GridView 时,我的应用程序不断崩溃。

04-14 09:18:50.704 6903-6903/com.example.bernine.practicalsessions E/AndroidRuntime: 致命异常: main java.lang.ClassCastException: android.widget.GridView 不能转换为 android.widget.TextView在 com.example.bernine.practicalsessions.TechnologyFragment.onCreateView(Technology‌​Fragment.java:38)

我在 XML 文件中做错了吗?

<?xml version="1.0" encoding="utf-8"?>
<!--<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center" />-->

<GridView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gridview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:columnWidth="90dp"
    android:numColumns="auto_fit"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:stretchMode="columnWidth"
    android:gravity="center"
    />

这是片段类

public class ScienceFragment extends Fragment {

    public static final String ARG_PAGE = "ARG_PAGE";

    private int mPage;

    public static ScienceFragment newInstance(int page) {
        Bundle args = new Bundle();
        args.putInt(ARG_PAGE, page);
        ScienceFragment fragment = new ScienceFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPage = getArguments().getInt(ARG_PAGE);
        Toast.makeText(getContext(), "Science", Toast.LENGTH_SHORT).show();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_sports_fragment, container, false);
        TextView textView = (TextView) view;
        textView.setText("Fragment Science #" + mPage);
        return view;
    }
}

当 textView 被取消注释并且 gridview 被注释时,应用程序运行良好。

【问题讨论】:

  • 能否提供错误的stacktrace?
  • 04-14 09:18:50.704 6903-6903/com.example.bernine.practicalsessions E/AndroidRuntime: 致命异常: main java.lang.ClassCastException: android.widget.GridView 无法转换为android.widget.TextView 在 com.example.bernine.practicalsessions.TechnologyFragment.onCreateView(TechnologyFragment.java:38) =
  • 这意味着您正在尝试“将 GridView 转换为 TextView”。这样做:GridView gView = (GridView) view;
  • 然后尝试将此行 TextView textView = (TextView) view; 更改为 GridView gridView = (GridView ) view;

标签: android fragment


【解决方案1】:

从您的Fragment 中删除这部分代码

TextView textView = (TextView) view;
textView.setText("Fragment Science #" + mPage);

而不是那把你的GridView 逻辑

【讨论】:

    【解决方案2】:

    从代码中删除您的 TextView 并声明您的 GridView 并为您的 GridView 制作适配器。

     TextView textView = (TextView) view;
        textView.setText("Fragment Science #" + mPage);
    
    main java.lang.ClassCastException: android.widget.GridView cannot be cast to android.widget.TextView ....
    

    这说明你还在用Textview

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-26
    • 1970-01-01
    • 1970-01-01
    • 2014-05-04
    • 2014-06-05
    • 2023-03-13
    • 1970-01-01
    相关资源
    最近更新 更多