1
votes

sometimes my app get a crash. I dont no why it doesent work sometimes, i didnt change anything. In my fragment i add 2 items on onViewCreated()

listview_products = (ListView) view.findViewById(R.id.listview_product_list);
mSwipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.productlist_swipe_refresh);

The error is this near (it's line 60)listview_products = (ListView) view.findViewById(R.id.listview_product_list);

This is my layout file:

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

<android.support.v4.widget.SwipeRefreshLayout
    android:id="@+id/productlist_swipe_refresh"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/listview_product_list"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
</android.support.v4.widget.SwipeRefreshLayout>

Why i get the error? What can i do to fix it?

01-31 14:50:09.583 22103-22103/de.app.me.de E/AndroidRuntime: FATAL EXCEPTION: main Process: de.app.me.de, PID: 22103 java.lang.RuntimeException: Unable to start activity ComponentInfo{de.app.me.de/de.app.me.de.account_overview}: java.lang.ClassCastException: android.support.v4.widget.SwipeRefreshLayout cannot be cast to android.widget.ListView at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2658) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2723) at android.app.ActivityThread.access$900(ActivityThread.java:172) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:145) at android.app.ActivityThread.main(ActivityThread.java:5832) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194) Caused by: java.lang.ClassCastException: android.support.v4.widget.SwipeRefreshLayout cannot be cast to android.widget.ListView at de.app.me.de.FragmentProductList.onViewCreated(FragmentProductList.java:60) at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:918) at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1082) at android.app.BackStackRecord.run(BackStackRecord.java:833) at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467) at android.app.Activity.performStart(Activity.java:6257) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2621) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2723)  at android.app.ActivityThread.access$900(ActivityThread.java:172)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:145)  at android.app.ActivityThread.main(ActivityThread.java:5832)  at java.lang.reflect.Method.invoke(Native Method)  at java.lang.reflect.Method.invoke(Method.java:372)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194) 

Thanks for help!

EDIT:

package de.app.me.de;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.ProgressDialog;
import android.content.ClipData;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import org.json.simple.ItemList;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import api.Request;
import classes.app.me.de.ListViewCustomAdapter;
import classes.app.me.de.ListViewItem;

/**
 * Created by ME on 15.01.2016.
 */
public class FragmentProductList extends Fragment{

    public FragmentProductList() {}
    private Context context;
    private TextView textView;
    private View rootView;
    private Request r;
    public ArrayList<ListViewItem> arrayList = new ArrayList<ListViewItem>();
    private ListView listview_products;
    SwipeRefreshLayout mSwipeRefreshLayout;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        this.rootView = inflater.inflate(R.layout.fragment_productlist, container, false);
        this.context = rootView.getContext();


        return this.rootView;
    }


    @Override
    public void onViewCreated(View view, Bundle saBundle) {
        getActivity().setTitle(R.string.title_activity_account_plist);

        listview_products = (ListView) view.findViewById(R.id.listview_product_list);
        mSwipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.productlist_swipe_refresh);

        listview_products.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Bundle bundle = new Bundle();

                bundle.putInt("pid", arrayList.get(position).getId());

                Fragment f = new FragmentProductManager();
                f.setArguments(bundle);

                FragmentManager fm = getFragmentManager();
                fm.beginTransaction().replace(R.id.frame_container, f).commit();

            }
        });


        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {

                Update();

            }
        });

        Update();
    }

    public void Update () {

        arrayList.clear();

        new GetProductList().execute();
        mSwipeRefreshLayout.setRefreshing(false);

    }

    public void UpdateList (ArrayList list) {

        ListViewCustomAdapter adapter = new ListViewCustomAdapter(rootView.getContext(), list);

        listview_products.setAdapter(adapter);


    }

    private class GetProductList extends AsyncTask<String, Void, Boolean> {

        private ProgressDialog pd;

        private android.app.AlertDialog.Builder dlgAlert;
        @Override
        protected Boolean doInBackground(String... params) {

            ...

        }

        @Override
        protected void onPreExecute() {

            ...

        }

        @Override
        protected void onPostExecute(Boolean status) {

        ...

        }
    }

}
1
can you post more code FragmentProductList.javathedarkpassenger

1 Answers

0
votes

Move this code

    listview_products = (ListView) rootView.findViewById(R.id.listview_product_list);
mSwipeRefreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.productlist_swipe_refresh);

inside onCreateView() method instead of onViewCreated()