Search code examples
androidindexoutofboundsexception

ListFragment in android without having listview


This Android code for MainActivity but its not working in my case but from where i copied it working fine there so please tell where i messed up.

I am new to Android development so please help me.

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

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/Onlinechat"
            android:textColor="@android:color/white"
            android:textStyle="bold"
            android:background="@color/colorPrimary"
            android:padding="12dp"
            android:gravity="center"
            android:textSize="22sp"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/Onlinechat"
            android:textSize="18sp"
            android:textColor="@color/colorAccent"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"/>

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <fragment
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/fragmentlist"
                android:name="es.esy.buddy.com.simplechat.ListOfOnlineMember"
                tools:layout="@layout/listviewonlinitems" />

        </ScrollView>
    </LinearLayout>

Code for Fragment list item which we where going to show in fragment at runtime.

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal" android:layout_width="match_parent"
        android:id="@+id/Listitem1"
        android:padding="8dp"
        android:layout_height="match_parent">

        <ImageView
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:id="@+id/imageviewshow"
            android:src="@drawable/wallpaper6"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:orientation="vertical">
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="20sp"
                android:text="Tuneer Mahatpure"
                android:gravity="start"
                android:id="@+id/txtName"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="20sp"
                android:id="@+id/txtNumber"
                android:text="7828743317"
                android:gravity="start|bottom"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"/>

        </LinearLayout>

    </LinearLayout>

Java Code for fragment list but i want to use listview for this purpose but i am not able to intialise listview in fragment java code i want to know is there any way to use listview in fragment java code

    package es.esy.buddy.com.simplechat;


    import android.app.ListFragment;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.SimpleAdapter;

    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;


    /**
     * Created by Tuneer on 07-03-2017.
     */

    public class ListOfOnlineMember extends ListFragment {

        String[] countries = new String[] {
                "India",
                "Pakistan",
                "Sri Lanka",
                "China",
                "Bangladesh",
                "Nepal",
                "Afghanistan",
                "North Korea",
                "South Korea",

        };

        // Array of integers points to images stored in /res/drawable/
        int[] flags = new int[]{
                R.drawable.picture1,
                R.drawable.picture2,
                R.drawable.picture3,
                R.drawable.wallpaper2,
                R.drawable.wallpaper3,
                R.drawable.wallpaper5,
                R.drawable.wallpaper4,
                R.drawable.wallpaper6,
                R.drawable.wallpaper7,
        };

        // Array of strings to store currencies
        String[] currency = new String[]{
                "Indian Rupee",
                "Pakistani Rupee",
                "Sri Lankan Rupee",
                "Renminbi",
                "Bangladeshi Taka",
                "Nepalese Rupee",
                "Afghani",
                "North Korean Won",
                "South Korean Won",
        };

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

            // Each row in the list stores country name, currency and flag
            List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();

            for(int i=0;i<10;i++){
                HashMap<String, String> hm = new HashMap<String,String>();
                hm.put("txt", countries[i]);
                hm.put("cur", currency[i]);
                hm.put("flag", Integer.toString(flags[i]) );
                aList.add(hm);
            }

            // Keys used in Hashmap
            String[] from = { "flag","txt","cur" };

            // Ids of views in listview_layout
            int[] to = { R.id.imageviewshow,R.id.txtName,R.id.txtNumber};

            // Instantiating an adapter to store each items
            // R.layout.listview_layout defines the layout of each item
            SimpleAdapter adapter = new SimpleAdapter(getActivity().getBaseContext(), aList, R.layout.listviewonlinitems, from, to);

            setListAdapter(adapter);

            return super.onCreateView(inflater, container, savedInstanceState);
        }
    }

    #this is Java code for MainActivity.java what i write here is simple code just calling layout to show up but the app is not opening or its suddenly stopped whenever my code goes on this activity. 
    package es.esy.buddy.com.simplechat;

    import android.app.Activity;
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentActivity;

    /**
     * Created by Tuneer on 07-03-2017.
     */

    public class OnlineChatMember extends FragmentActivity {

        @Override
        public void onCreate(Bundle SavedInstanceState){
            super.onCreate(SavedInstanceState);
            setContentView(R.layout.onlinechatmember);
        }
    }

    #Errors i am getting Errors while opening the app on this activity showing this error but i used carefull with array then why it is showing array errors.

    03-09 04:21:27.399 6846-6846/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                     Process: es.esy.buddy.com.simplechat, PID: 6846
                                                     java.lang.RuntimeException: Unable to start activity ComponentInfo{es.esy.buddy.com.simplechat/es.esy.buddy.com.simplechat.OnlineChatMember}: android.view.InflateException: Binary XML file line #30: Binary XML file line #30: Error inflating class fragment
                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
                                                         at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
                                                         at android.os.Handler.dispatchMessage(Handler.java:102)
                                                         at android.os.Looper.loop(Looper.java:154)
                                                         at android.app.ActivityThread.main(ActivityThread.java:6077)
                                                         at java.lang.reflect.Method.invoke(Native Method)
                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
                                                      Caused by: android.view.InflateException: Binary XML file line #30: Binary XML file line #30: Error inflating class fragment
                                                      Caused by: android.view.InflateException: Binary XML file line #30: Error inflating class fragment
                                                      Caused by: java.lang.ArrayIndexOutOfBoundsException: length=9; index=9
                                                         at es.esy.buddy.com.simplechat.ListOfOnlineMember.onCreateView(ListOfOnlineMember.java:69)
                                                         at android.app.Fragment.performCreateView(Fragment.java:2353)
                                                         at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:959)
                                                         at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1149)
                                                         at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1251)
                                                         at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2311)
                                                         at android.app.FragmentController.onCreateView(FragmentController.java:98)
                                                         at android.app.Activity.onCreateView(Activity.java:5884)
                                                         at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:36)
                                                         at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:77)
                                                         at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:777)
                                                         at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
                                                         at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
                                                         at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
                                                         at android.view.LayoutInflater.rInflate(LayoutInflater.java:861)
                                                         at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
                                                         at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
                                                         at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
                                                         at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
                                                         at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:414)
                                                         at android.app.Activity.setContentView(Activity.java:2414)
                                                         at es.esy.buddy.com.simplechat.OnlineChatMember.onCreate(OnlineChatMember.java:17)
                                                         at android.app.Activity.performCreate(Activity.java:6662)
                                                         at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
                                                         at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
                                                         at android.os.Handler.dispatchMessage(Handler.java:102)
                                                         at android.os.Looper.loop(Looper.java:154)
                                                         at android.app.ActivityThread.main(ActivityThread.java:6077)
                                                         at java.lang.reflect.Method.invoke(Native Method)
                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

This is it so can u help me

Solution

  • The stack trace states Caused by: java.lang.ArrayIndexOutOfBoundsException: length=9; index=9

    You are calling this code

    for(int i=0;i<10;i++){
        HashMap<String, String> hm = new HashMap<String,String>();
        hm.put("txt", countries[i]);
        hm.put("cur", currency[i]);
        hm.put("flag", Integer.toString(flags[i]) );
        aList.add(hm);
    }
    

    Your various arrays (countries, currency, flags) have 9 elements, yet you clearly loop 10 times, hence you are getting the exception. I would suggesting replace 10 with countries.length

    This is super simple java and really has nothing to do with the question asked. Simply readying the stack trace would have pointed you in the right direction.