gpt4 book ai didi

android - 可扩展列表适配器的问题

转载 作者:搜寻专家 更新时间:2023-11-01 08:15:55 24 4
gpt4 key购买 nike

我是Android开发的新手,所以我希望有人能帮助我解决这个问题。

我正在尝试创建一个可扩展列表,我尝试使用谷歌搜索并通读谷歌文档。但不知何故,我无法全神贯注。我的代码在 Eclipse 中没有给出任何错误。但是在模拟器中运行它时,它会在启动时崩溃。当我将适配器 设置为 View 时,我认为我做错了什么。

这是我的代码:

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.ExpandableListView;
import android.widget.ExpandableListAdapter;
import android.widget.BaseExpandableListAdapter;
import android.view.LayoutInflater;
import android.content.Context;

public class hmenyActivity extends MyActivity {

ExpandableListAdapter adapter;

private Context context;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.hmeny);

adapter = new MyExpandableListAdapter();

ExpandableListView listView = (ExpandableListView) findViewById(R.id.listView);

listView.setOnChildClickListener(new OnChildClickListener()
{
@Override
public boolean onChildClick(ExpandableListView arg0, View arg1, int arg2, int arg3, long arg4)
{
Toast.makeText(getBaseContext(), "Child clicked", Toast.LENGTH_LONG).show();
return false;
}

});


listView.setAdapter(adapter);

}

public class MyExpandableListAdapter extends BaseExpandableListAdapter {

private String[] groups = {
"Test1",
"Test2",
"Test3"
};

private String[][] children = {

{
"foo1",
"foo2",
"foo3"
},

{
"foo1",
"foo2",
"foo3"
},

{
"foo1",
"foo2",
"foo3"
}
};

public Object getChild(int groupPosition, int childPosition) {
return children[groupPosition][childPosition];
}

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

public int getChildrenCount(int groupPosition) {
return children[groupPosition].length;
}

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

if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.underkat_layout, null);
}

TextView textView = (TextView) convertView.findViewById(R.id.tvChild);
textView.setText(getChild(groupPosition, childPosition).toString());
return convertView;
}

public Object getGroup(int groupPosition) {
return groups[groupPosition];
}

public int getGroupCount() {
return groups.length;
}

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

public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
ViewGroup parent) {
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.kat_layout, null);
}
TextView textView = (TextView) convertView.findViewById(R.id.tvGroup);
textView.setText(getGroup(groupPosition).toString());
return convertView;
}

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

public boolean hasStableIds() {
return true;
}
}
}

@ingo 这里是 logcat 输出:

02-14 14:49:26.673: ERROR/AndroidRuntime(302): Uncaught handler: thread main exiting due to uncaught exception
02-14 14:49:26.693: ERROR/AndroidRuntime(302): java.lang.NullPointerException
02-14 14:49:26.693: ERROR/AndroidRuntime(302): at com.android.quizapp.hmenyActivity$MyExpandableListAdapter.getGroupView(hmenyActivity.java:151)
02-14 14:49:26.693: ERROR/AndroidRuntime(302): at android.widget.ExpandableListConnector.getView(ExpandableListConnector.java:446)
02-14 14:49:26.693: ERROR/AndroidRuntime(302): at android.widget.AbsListView.obtainView(AbsListView.java:1273)
02-14 14:49:26.693: ERROR/AndroidRuntime(302): at android.widget.ListView.measureHeightOfChildren(ListView.java:1147)
02-14 14:49:26.693: ERROR/AndroidRuntime(302): at android.widget.ListView.onMeasure(ListView.java:1060)
02-14 14:49:26.693: ERROR/AndroidRuntime(302): at android.view.View.measure(View.java:7703)
02-14 14:49:26.693: ERROR/AndroidRuntime(302): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:2989)
02-14 14:49:26.693: ERROR/AndroidRuntime(302): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:888)
02-14 14:49:26.693: ERROR/AndroidRuntime(302): at android.widget.LinearLayout.measureVertical(LinearLayout.java:350)
02-14 14:49:26.693: ERROR/AndroidRuntime(302): at android.widget.LinearLayout.onMeasure(LinearLayout.java:278)
02-14 14:49:26.693: ERROR/AndroidRuntime(302): at android.view.View.measure(View.java:7703)
02-14 14:49:26.693: ERROR/AndroidRuntime(302): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:2989)
02-14 14:49:26.693: ERROR/AndroidRuntime(302): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
02-14 14:49:26.693: ERROR/AndroidRuntime(302): at android.view.View.measure(View.java:7703)
02-14 14:49:26.693: ERROR/AndroidRuntime(302): at android.widget.LinearLayout.measureVertical(LinearLayout.java:464)
02-14 14:49:26.693: ERROR/AndroidRuntime(302): at android.widget.LinearLayout.onMeasure(LinearLayout.java:278)
02-14 14:49:26.693: ERROR/AndroidRuntime(302): at android.view.View.measure(View.java:7703)
02-14 14:49:26.693: ERROR/AndroidRuntime(302): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:2989)
02-14 14:49:26.693: ERROR/AndroidRuntime(302): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
02-14 14:49:26.693: ERROR/AndroidRuntime(302): at android.view.View.measure(View.java:7703)
02-14 14:49:26.693: ERROR/AndroidRuntime(302): at android.view.ViewRoot.performTraversals(ViewRoot.java:747)
02-14 14:49:26.693: ERROR/AndroidRuntime(302): at android.view.ViewRoot.handleMessage(ViewRoot.java:1613)
02-14 14:49:26.693: ERROR/AndroidRuntime(302): at android.os.Handler.dispatchMessage(Handler.java:99)
02-14 14:49:26.693: ERROR/AndroidRuntime(302): at android.os.Looper.loop(Looper.java:123)
02-14 14:49:26.693: ERROR/AndroidRuntime(302): at android.app.ActivityThread.main(ActivityThread.java:4203)
02-14 14:49:26.693: ERROR/AndroidRuntime(302): at java.lang.reflect.Method.invokeNative(Native Method)
02-14 14:49:26.693: ERROR/AndroidRuntime(302): at java.lang.reflect.Method.invoke(Method.java:521)
02-14 14:49:26.693: ERROR/AndroidRuntime(302): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
02-14 14:49:26.693: ERROR/AndroidRuntime(302): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
02-14 14:49:26.693: ERROR/AndroidRuntime(302): at dalvik.system.NativeStart.main(Native Method)
02-14 14:49:26.713: INFO/Process(51): Sending signal. PID: 302 SIG: 3
02-14 14:49:26.713: INFO/dalvikvm(302): threadid=7: reacting to signal 3
02-14 14:49:26.713: ERROR/dalvikvm(302): Unable to open stack trace file '/data/anr/traces.txt': Permission denied
02-14 14:49:36.493: WARN/ActivityManager(51): Launch timeout has expired, giving up wake lock!
02-14 14:49:36.506: WARN/ActivityManager(51): Activity idle timeout for HistoryRecord{43919b58 com.android.skiquiz/.hmenyActivity}
02-14 14:49:41.533: WARN/ActivityManager(51): Activity destroy

最佳答案

我想通了。对于任何感兴趣的人,您所要做的就是将“Context”声明放在“MyExpandableListAdapter”中,并将“this”添加到适配器声明中。请参阅下面的代码:

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.ExpandableListView;
import android.widget.ExpandableListAdapter;
import android.widget.BaseExpandableListAdapter;
import android.view.LayoutInflater;
import android.content.Context;

public class hmenyActivity extends MyActivity {

ExpandableListAdapter adapter;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.hmeny);

adapter = new MyExpandableListAdapter(this);

ExpandableListView listView = (ExpandableListView) findViewById(R.id.listView);

listView.setOnChildClickListener(new OnChildClickListener()
{
@Override
public boolean onChildClick(ExpandableListView arg0, View arg1, int arg2, int arg3, long arg4)
{
Toast.makeText(getBaseContext(), "Child clicked", Toast.LENGTH_LONG).show();
return false;
}

});


listView.setAdapter(adapter);

}

public class MyExpandableListAdapter extends BaseExpandableListAdapter {

private String[] groups = {
"Test1",
"Test2",
"Test3"
};

private String[][] children = {

{
"foo1",
"foo2",
"foo3"
},

{
"foo1",
"foo2",
"foo3"
},

{
"foo1",
"foo2",
"foo3"
}
};

//Add these lines to your code
private Context context;

public MyExpandableListAdapter(Context context) {
this.context = context;
}

public Object getChild(int groupPosition, int childPosition) {
return children[groupPosition][childPosition];
}

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

public int getChildrenCount(int groupPosition) {
return children[groupPosition].length;
}

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

if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.underkat_layout, null);
}

TextView textView = (TextView) convertView.findViewById(R.id.tvChild);
textView.setText(getChild(groupPosition, childPosition).toString());
return convertView;
}

public Object getGroup(int groupPosition) {
return groups[groupPosition];
}

public int getGroupCount() {
return groups.length;
}

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

public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
ViewGroup parent) {
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.kat_layout, null);
}
TextView textView = (TextView) convertView.findViewById(R.id.tvGroup);
textView.setText(getGroup(groupPosition).toString());
return convertView;
}

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

public boolean hasStableIds() {
return true;
}
}
}

关于android - 可扩展列表适配器的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4992797/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com