2016-03-18 11 views
0

Uygulamayı derlemeye çalışırken aynı hatayı almaya devam ediyorum, Uygulama listenin Grubunda bir soru ve her grubun alt öğesindeki 5 görüntüyü göstermelidir. Hatanın Bağlamda olabileceğini düşündüm.Resimlerle GenişletilebilirListAdaptide Hata

MainActivity.java

public class MainActivity extends FragmentActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    if (findViewById(R.id.fragment_container) != null) { 

     // However, if we're being restored from a previous state, 
     // then we don't need to do anything and should return or else 
     // we could end up with overlapping fragments. 
     if (savedInstanceState != null) { 
      return; 
     } 

     // Create a new Fragment to be placed in the activity layout 
     QuestionsFragment firstFragment = new QuestionsFragment(); 

     firstFragment.setArguments(getIntent().getExtras()); 

     // Add the fragment to the 'fragment_container' FrameLayout 
     getFragmentManager().beginTransaction() 
       .add(R.id.fragment_container, firstFragment).commit(); 
    } 
} 

} 

QuestionsFragment.java

public class QuestionsFragment extends Fragment { 

ExpandableListAdapter listAdapter; 
ExpandableListView expListView; 
List<String> listDataHeader; 
HashMap<String, List<Integer>> listDataChild; 

//private ArrayAdapter<String> mQuestionAdapter; 

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

    View rootView = inflater.inflate(R.layout.questions_fragment, container, false); 

    //Get the ListView 
    expListView = (ExpandableListView) rootView.findViewById(R.id.list_view); 

    //Prepare ListData 
    prepareListData(); 

    listAdapter = new ExpandableListAdapter(QuestionsFragment.this.getActivity().getApplicationContext(), listDataHeader, listDataChild); 

    expListView.setAdapter((android.widget.ExpandableListAdapter) listAdapter); 


    // Inflate the layout for this fragment 
    return rootView; 
} 


/* 
* Preparing the list data 
*/ 
private void prepareListData() { 
    listDataHeader = new ArrayList<String>(); 
    listDataChild = new HashMap<String, List<Integer>>(); 

    // Adding child data 
    listDataHeader.add("Question 1"); 
    listDataHeader.add("Question 2"); 
    listDataHeader.add("Question 3"); 

    // Adding child data 
    List<Integer> emoticons = new ArrayList<Integer>(); 
    emoticons.add(R.drawable.bad); 
    emoticons.add(R.drawable.bad2); 
    emoticons.add(R.drawable.normal); 
    emoticons.add(R.drawable.good); 
    emoticons.add(R.drawable.excelent); 

    listDataChild.put(listDataHeader.get(0), emoticons); // Header, Child data 
    listDataChild.put(listDataHeader.get(1), emoticons); 
    listDataChild.put(listDataHeader.get(2), emoticons); 
} 
} 

ExpandableListAdapter.java

public class ExpandableListAdapter { 

    private Context _context; 
    private List<String> _listDataHeader; // header titles 
    // child data in format of header title, child title 
    private HashMap<String, List<Integer>> _listDataChild; 

    public ExpandableListAdapter(Context context, List<String> listDataHeader, 
           HashMap<String, List<Integer>> listChildData) { 
     this._context = context; 
     this._listDataHeader = listDataHeader; 
     this._listDataChild = listChildData; 
    } 

    public Object getChild(int groupPosition, int childPosititon) { 
     return this._listDataChild.get(this._listDataHeader.get(groupPosition)) 
       .get(childPosititon); 
    } 

    public long getChildId(int groupPosition, int childPosition) { 
     return childPosition; 
    } 

    public View getChildView(int groupPosition, final int childPosition, 
          boolean isLastChild, View convertView, ViewGroup parent) { 


     //final String childText = (String) getChild(groupPosition, childPosition); 

     if (convertView == null) { 
      LayoutInflater infalInflater = (LayoutInflater) this._context 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = infalInflater.inflate(R.layout.emoticons_question_list_item, null); 
     } 


     ImageView lblListChild = (ImageView) convertView 
       .findViewById(R.id.image_bad); 

     ImageView lblListChild2 = (ImageView) convertView 
       .findViewById(R.id.image_bad2); 

     ImageView lblListNormal = (ImageView) convertView 
       .findViewById(R.id.image_normal); 

     ImageView lblListGood = (ImageView) convertView 
       .findViewById(R.id.image_good); 

     ImageView lblListExcelent = (ImageView) convertView 
       .findViewById(R.id.image_excelent); 

     TextView txtListChild = (TextView) convertView 
       .findViewById(R.id.question_list_item_textview); 

     return convertView; 
    } 

    public int getChildrenCount(int groupPosition) { 
     return this._listDataChild.get(this._listDataHeader.get(groupPosition)) 
       .size(); 
    } 

    public Object getGroup(int groupPosition) { 
     return this._listDataHeader.get(groupPosition); 
    } 

    public int getGroupCount() { 
     return this._listDataHeader.size(); 
    } 

    public long getGroupId(int groupPosition) { 
     return groupPosition; 
    } 

    public View getGroupView(int groupPosition, boolean isExpanded, 
          View convertView, ViewGroup parent) { 
     String headerTitle = (String) getGroup(groupPosition); 
     if (convertView == null) { 
      LayoutInflater infalInflater = (LayoutInflater) this._context 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = infalInflater.inflate(R.layout.question_list_item, null); 
     } 

     TextView txtListGroup = (TextView) convertView 
       .findViewById(R.id.question_list_item_textview); 
     txtListGroup.setText(headerTitle); 

     return convertView; 
    } 

    public boolean hasStableIds() { 
     return false; 
    } 

    public boolean isChildSelectable(int groupPosition, int childPosition) { 
     return true; 
    } 
    } 

questions_fragment.xml

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

    <ExpandableListView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:divider="@drawable/list_divider" 
     android:dividerHeight="1dp" 
     android:id="@+id/list_view"> 
    </ExpandableListView> 
</FrameLayout> 

question_list_item.xml

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

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:minHeight="10dp" 
     android:gravity="center" 
     android:id="@+id/question_list_item_textview" 
     android:textColor="@color/black" 
     android:textSize="21dp"> 

    </TextView> 

emoticons_question_list_item.xml

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

    <TableLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_margin="30dp" 
     android:stretchColumns="*"> 
     <!-- android:padding="2dp"> --> 

     <TableRow> 

      <ImageView 
       android:layout_height="55dp" 
       android:layout_width="55dp" 
       android:id="@+id/image_bad"/> 

      <ImageView 
       android:layout_height="55dp" 
       android:layout_width="55dp" 
       android:id="@+id/image_bad2"/> 

      <ImageView 
       android:layout_height="55dp" 
       android:layout_width="55dp" 
       android:id="@+id/image_normal"/> 

      <ImageView 
       android:layout_height="55dp" 
       android:layout_width="55dp" 
       android:id="@+id/image_good"/> 

      <ImageView 
       android:layout_height="55dp" 
       android:layout_width="55dp" 
       android:src="@+id/image_excelent"/> 

     </TableRow> 

    </TableLayout> 


</LinearLayout> 

Bu döndüren Hata geçerli:

java.lang.RuntimeException: Unable to start activity ComponentInfo{br.ind.uptech.princessfeedback/br.ind.uptech.princessfeedback.MainActivity}: java.lang.ClassCastException: br.ind.uptech.princessfeedback.ExpandableListAdapter cannot be cast to android.widget.ExpandableListAdapter 
                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3139) 
                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3238) 
                       at android.app.ActivityThread.access$1000(ActivityThread.java:197) 
                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1681) 
                       at android.os.Handler.dispatchMessage(Handler.java:102) 
                       at android.os.Looper.loop(Looper.java:145) 
                       at android.app.ActivityThread.main(ActivityThread.java:6862) 
                       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:1404) 
                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199) 
                      Caused by: java.lang.ClassCastException: br.ind.uptech.princessfeedback.ExpandableListAdapter cannot be cast to android.widget.ExpandableListAdapter 
                       at br.ind.uptech.princessfeedback.QuestionsFragment.onCreateView(QuestionsFragment.java:44) 
                       at android.app.Fragment.performCreateView(Fragment.java:2114) 
                       at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:904) 
                       at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1082) 
                       at android.app.BackStackRecord.run(BackStackRecord.java:834) 
                       at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467) 
                       at android.app.Activity.performStart(Activity.java:6573) 
                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3102) 
                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3238)  
                       at android.app.ActivityThread.access$1000(ActivityThread.java:197)  
                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1681)  
                       at android.os.Handler.dispatchMessage(Handler.java:102)  
                       at android.os.Looper.loop(Looper.java:145)  
                       at android.app.ActivityThread.main(ActivityThread.java:6862)  
                       at java.lang.reflect.Method.invoke(Native Method)  

çok teşekkürler!

cevap

0

Değiştir

expListView.setAdapter((android.widget.ExpandableListAdapter) listAdapter); 

expListView.setAdapter(listAdapter); 
+0

I değişti ve aşağıdaki hata var: 'hata: (44, 20) bir hata: setAdapter Bulunan herhangi bir uygun yöntem (br .ind.uptech.princessfeedback.ExpandableListAdapter) yöntemi ExpandableListView.setAdapter (android.widget.ExpandableListAdapter) uygulanamaz (asıl argüman br.ind.uptech.princessfeedback.Expandable ListAdapter metot çağırma dönüşümü ile android.widget.ExpandableListAdapter'a dönüştürülemez) yöntemi ExpandableListView.setAdapter (ListAdapter) uygulanamaz ' –

+0

problemi, Android çerçevesindeki adıyla aynı olan ExpandableListAdapter sınıfını tanımlamanızdır. İthalatta çalışmalısın. – Lino

+0

Evet haklıydın, teşekkürler! Sorunumu çözdüm –