2017-03-08 46 views
-2

這是MainActivity的Android代碼,但它不工作在我的情況下 但從我複製它在那裏工作很好,所以請告訴我在哪裏搞砸了。Android中的ListFragment沒有列表視圖

我是Android開發新手,請幫助我。

<?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> 

代碼片段列表項目,我們將在運行時在片段中顯示。

<?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代碼片段列表中,但我想使用列表視圖用於此目的,但我不能夠在片段的Java代碼,我想知道intialise列表視圖中,有沒有辦法在片段的Java代碼中使用的ListView

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 

回答

1

堆棧跟蹤狀態Caused by: java.lang.ArrayIndexOutOfBoundsException: length=9; index=9

要調用此代碼

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); 
} 

你的各種數組(國家,貨幣,國旗)有9個元素,但你明確地循環10次,因此你得到了例外。我會建議替換與countries.length

這是超級簡單的java和真正無關的問題問。簡單地準備好堆棧跟蹤將會指出你正確的方向。

+0

thnkss,但它不起作用 –

+0

它的工作原理thnks我是白癡我用一個代碼來完成所有的活動,當國家沒有空值...這就是爲什麼應用程序或活動開始關閉每次我打開它....喬·馬赫你救了我 –