原谅我可能是最愚蠢的问题,但我不知道是什么导致我的应用程序崩溃(这一次)。我只是试图将xml布局文件扩展为可扩展列表视图。
package com.anthonyce.mcathomie;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;
public class PlayoptionsActivity extends Activity {
ExpandableListView Mtopics;
ExpandableAdapter MtopicsAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_playoptions);
//set up adapter
MtopicsAdapter = new ExpandableAdapter();
Mtopics = (ExpandableListView) findViewById(R.id.mtopicsListView);
Mtopics.setAdapter(MtopicsAdapter);
//Mtopics.setGroupIndicator(null);
}
public class ExpandableAdapter extends BaseExpandableListAdapter {
private Context context;
private String[] groups = { "People Names", "Dog Names", "Cat Names",
"Fish Names" };
private String[][] children = { { "Arnold" }, { "Ace" }, { "Fluffy" },
{ "Goldy" } };
public Object getChild(int groupPosition, int childPosition) {
return children[groupPosition][childPosition];
}
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
View childView;
LayoutInflater childInflate = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
childView = childInflate.inflate(R.layout.mtopics_childrow, parent, false);
TextView childtxt = (TextView)childView.findViewById(R.id.mtopicchildtext);
childtxt.setText(getChild(groupPosition, childPosition).toString());
return childView;
}
public int getChildrenCount(int groupPosition) {
return children[groupPosition].length;
}
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) {
View groupView;
groupView = new View(context);
LayoutInflater groupInflate = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
groupView = groupInflate.inflate(R.layout.mtopics_grouprow, parent, false);
TextView grouptxt = (TextView)groupView.findViewById(R.id.mtopicgrouptext);
grouptxt.setText(getGroup(groupPosition).toString());
return groupView;
}
public boolean hasStableIds() {
return true;
}
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
}
这是我试图充气的组布局的xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mtopicgrouplin"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="fill">
<TextView
android:gravity="left"
android:id="@+id/mtopicgrouptext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:textStyle="bold" />
<CheckBox
android:id="@+id/checkBoxgroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
在它工作之前但没有显示任何文本,现在使用此代码它只是崩溃了。 我不认为这个问题会出现在这个问题上,但是这里就是
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mtopicchildlin"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="fill">
<CheckBox
android:paddingLeft="10dp"
android:id="@+id/checkBoxchild"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/mtopicchildtext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
这是我发现难以辨认的logCat
09-05 17:09:33.919: E/AndroidRuntime(429): FATAL EXCEPTION: main
09-05 17:09:33.919: E/AndroidRuntime(429): java.lang.NullPointerException
09-05 17:09:33.919: E/AndroidRuntime(429): at android.view.ViewConfiguration.get(ViewConfiguration.java:228)
09-05 17:09:33.919: E/AndroidRuntime(429): at android.view.View.<init>(View.java:1878)
09-05 17:09:33.919: E/AndroidRuntime(429): at com.anthonyce.mcathomie.PlayoptionsActivity$ExpandableAdapter.getGroupView(PlayoptionsActivity.java:118)
09-05 17:09:33.919: E/AndroidRuntime(429): at android.widget.ExpandableListConnector.getView(ExpandableListConnector.java:445)
09-05 17:09:33.919: E/AndroidRuntime(429): at android.widget.AbsListView.obtainView(AbsListView.java:1430)
09-05 17:09:33.919: E/AndroidRuntime(429): at android.widget.ListView.measureHeightOfChildren(ListView.java:1216)
09-05 17:09:33.919: E/AndroidRuntime(429): at android.widget.ListView.onMeasure(ListView.java:1127)
09-05 17:09:33.919: E/AndroidRuntime(429): at android.view.View.measure(View.java:8313)
09-05 17:09:33.919: E/AndroidRuntime(429): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
09-05 17:09:33.919: E/AndroidRuntime(429): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
09-05 17:09:33.919: E/AndroidRuntime(429): at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
09-05 17:09:33.919: E/AndroidRuntime(429): at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
09-05 17:09:33.919: E/AndroidRuntime(429): at android.view.View.measure(View.java:8313)
09-05 17:09:33.919: E/AndroidRuntime(429): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
09-05 17:09:33.919: E/AndroidRuntime(429): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
09-05 17:09:33.919: E/AndroidRuntime(429): at android.view.View.measure(View.java:8313)
09-05 17:09:33.919: E/AndroidRuntime(429): at android.widget.LinearLayout.measureVertical(LinearLayout.java:531)
09-05 17:09:33.919: E/AndroidRuntime(429): at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
09-05 17:09:33.919: E/AndroidRuntime(429): at android.view.View.measure(View.java:8313)
09-05 17:09:33.919: E/AndroidRuntime(429): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
09-05 17:09:33.919: E/AndroidRuntime(429): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
09-05 17:09:33.919: E/AndroidRuntime(429): at android.view.View.measure(View.java:8313)
09-05 17:09:33.919: E/AndroidRuntime(429): at android.view.ViewRoot.performTraversals(ViewRoot.java:839)
09-05 17:09:33.919: E/AndroidRuntime(429): at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
09-05 17:09:33.919: E/AndroidRuntime(429): at android.os.Handler.dispatchMessage(Handler.java:99)
09-05 17:09:33.919: E/AndroidRuntime(429): at android.os.Looper.loop(Looper.java:123)
09-05 17:09:33.919: E/AndroidRuntime(429): at android.app.ActivityThread.main(ActivityThread.java:3683)
09-05 17:09:33.919: E/AndroidRuntime(429): at java.lang.reflect.Method.invokeNative(Native Method)
09-05 17:09:33.919: E/AndroidRuntime(429): at java.lang.reflect.Method.invoke(Method.java:507)
09-05 17:09:33.919: E/AndroidRuntime(429): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-05 17:09:33.919: E/AndroidRuntime(429): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-05 17:09:33.919: E/AndroidRuntime(429): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
您永远不会为context
变量设置值,因此当您执行groupView = new View(context)
时,上下文为空并抛出NullPointerException
。
请改用当前活动上下文:
groupView = new View(PlayoptionsActivity.this);
您还可以创建构造函数:
ExpandableAdapter(Context context)
{
this.context = context;
}
答案 1 :(得分:1)
删除
groupView = new View(context);
并运行程序。