可扩展列表适配器的问题
我是Android开发的新手,所以我希望有人能帮助我解决这个问题。
我正在尝试创建一个可扩展的列表,我尝试过谷歌搜索,并阅读了谷歌文档。但不知怎的,我无法理解它。我的代码在 eclipse 中没有给出错误。但是当在模拟器中运行它时,它会在启动时崩溃。当我将适配器设置为视图时,我认为我做错了什么。
这是我的代码:
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
I am new to Android development, so I hope someone can help me with this problem.
I am trying to create a expandable list, I have tried googling, and read through googles documentation. But somehow I can not wrap my head around it. My code gives no errors in eclipse. But when running it in the emulator, it crashes on startup. I think I am doing something wrong when I set the adapter to the view.
Here is my code:
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 here is the logcat output:
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想通了。对于任何感兴趣的人,您所要做的就是将“Context”声明放入“MyExpandableListAdapter”中,并将“this”添加到适配器声明中。请参阅下面我的代码:
I figured it out. For anyone interested, all you have to do is put the "Context" declaration inside "MyExpandableListAdapter", and add "this" to the adapter declaration. See my code below: