ExpandableListView -UnsupportedOperationException : addView (View, LayoutParams)는 AdapterView에서 지원되지 않습니다.
Android에서 확장 가능한 목록보기를 구현하고 있는데 위의 제목이 붙은 오류가 발생합니다. 제발 도와주세요.
주요 활동은-
package com.expand;
import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.Toast;
public class MyExpandableListViewActivity extends Activity {
/** Called when the activity is first created. */
static final String groupElements[]= {
"India",
"Australia",
"England",
"South Africa"
};
static final String[][] childElements=
{
{
"Sachin Tendulkar",
"Raina",
"Dhoni",
"Yuvi"
},
{
"Ponting",
"Adam Gilchrist",
"Michael Clarke"
},
{
"Andrew Strauss",
"kevin Peterson",
"Nasser Hussain"
},
{
"Graeme Smith",
"AB de villiers",
"Jacques Kallis"
}
};
DisplayMetrics metrics;
int width;
ExpandableListView expandList;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
expandList = (ExpandableListView)findViewById(R.id.expandList1);
metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
width = metrics.widthPixels;
//ExpAdapter adapter = new ExpAdapter(MyExpandableListViewActivity.this, groupElements, childElements);
expandList.setAdapter(new ExpAdapter(MyExpandableListViewActivity.this, groupElements, childElements));
expandList.setIndicatorBounds(width - GetDipsFromPixel(50), width - GetDipsFromPixel(10));
expandList.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
// TODO Auto-generated method stub
Log.e("onGroupExpand", "OK");
}
});
expandList.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
@Override
public void onGroupCollapse(int groupPosition) {
// TODO Auto-generated method stub
Log.e("onGroupCollapse", "OK");
}
});
expandList.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
//getting the item that is selected
//String s = (String) expandList.getItemAtPosition((int) id);
Toast.makeText(MyExpandableListViewActivity.this, "You have selected :" , Toast.LENGTH_SHORT).show();
return false;
}
});
}
public int GetDipsFromPixel(float pixels)
{
// Get the screen's density scale
final float scale = getResources().getDisplayMetrics().density;
// Convert the dps to pixels, based on density scale
return (int) (pixels * scale + 0.5f);
}
}
ExpAdapter 클래스는-다른 클래스에서 어댑터를 구현했으며 mt 주요 활동에서 호출했습니다.
package com.expand;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
public class ExpAdapter extends BaseExpandableListAdapter {
public Context myContext;
public String[][] childElements;
TextView childValues;
public String[] groupElements;
public ExpAdapter(Context context, String[] group, String[][] childs)
{
this.myContext=context;
this.groupElements = group;
this.childElements = childs;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return childElements[groupPosition][childPosition];
}
@Override
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if(convertView==null){
LayoutInflater inflator = (LayoutInflater)myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflator.inflate(R.layout.child_rows, parent);
}
childValues = (TextView)convertView.findViewById(R.id.rowValues);
childValues.setText(childElements[groupPosition][childPosition]);
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
return groupElements[groupPosition].length();
}
@Override
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return groupElements[groupPosition];
}
@Override
public int getGroupCount() {
// TODO Auto-generated method stub
return groupElements.length;
}
@Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if(convertView==null){
LayoutInflater inflator = (LayoutInflater)myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflator.inflate(R.layout.group_rows, null);
}
TextView group = (TextView)convertView.findViewById(R.id.groupId);
group.setText(groupElements[groupPosition]);
return convertView;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return true;
}
}
main.xml-
이것은 주 활동에 의해 처음에 표시되는 xnl입니다.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ExpandableListView
android:id="@+id/expandList1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/android:empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
</TextView>
</ExpandableListView>
</LinearLayout>
group_row.xml
이것은 그룹 요소의 xml입니다.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gropu_name"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:orientation="vertical" >
<TextView
android:id="@+id/groupId"
android:layout_height="40dp"
android:layout_width="wrap_content"
android:paddingLeft="30dp"
android:gravity="center_vertical"
android:textSize="16dp"
android:textStyle="bold"
/>
</LinearLayout>
child_row.xml 하위 요소의 xml입니다.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:orientation="horizontal" >
<TextView
android:id="@+id/rowValues"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:gravity="center_vertical"
android:paddingLeft="50dp"
android:textSize="12dp" />
</LinearLayout>
Adapterview가 새보기 추가를 허용하지 않는 것 같습니다. 같은 문제가 발생했습니다.
다음 줄을 바꿔서 해결하십시오.
convertView = inflator.inflate(R.layout.child_rows, parent);
...에
convertView = inflator.inflate(R.layout.child_rows, null);
최신 정보
부모를 전혀 사용하지 않는 대신, Inflater에게 다음을 사용하여 부풀린 뷰를 부모에 연결하지 않도록 지시해야합니다.
convertView = inflator.inflate(R.layout.child_rows, parent, false);
이 답변 도 참조하십시오 .
The reason is that adapter takes care of attaching views to parent itself.
Note that you can also get this error when your layout xml is invalid.
As were noted above,
Instead of not using a parent at all, you should simply tell the Inflater not to attach the inflated view to the parent with
convertView = inflator.inflate(R.layout.child_rows, parent, false);
See also this answer.
The reason is that adapter takes care of attaching views to parent itself.
According to Android Lint your child view should always provide a reference to its parent view when inflated. I had the exact same error in my code. Is was occurring because the TextView was placed inside the ExpandableListView. When I rearranged my xml layout the error stopped appearing.
This error can also be caused because of instant run feature. I was working on listview and because of this error app kept crashing. Uninstalling the app and running again resolved the error.
'program story' 카테고리의 다른 글
Rails 3 : yield / content_for 일부 기본값? (0) | 2020.12.07 |
---|---|
void 함수에서 복귀 (0) | 2020.12.07 |
계속하려면 아무 키나 누르십시오. (0) | 2020.12.07 |
생성자를 호출하여 개체 다시 초기화 (0) | 2020.12.06 |
Python에서 소수의 효율적인 무한 생성기를 구현하는 방법은 무엇입니까? (0) | 2020.12.06 |