我正在创建使用可扩展列表视图的Android应用,每个子项都包含图片视图和扩展 BaseExpandableAdapter 的文本。
图像在drawable forlder中,我使用数组检索它。 当我尝试选择其中一个组来扩展它时,应用程序强制关闭并在 log cat 中显示我不理解的错误。
任何人都可以帮我解决这个错误????
03-23 20:21:15.927: E/AndroidRuntime(8513): FATAL EXCEPTION: main
03-23 20:21:15.927: E/AndroidRuntime(8513): java.lang.NullPointerException
03-23 20:21:15.927: E/AndroidRuntime(8513): at com.devleb.expandablelistdemo3.ExpandableAdapter.getChildView(ExpandableAdapter.java:129)
03-23 20:21:15.927: E/AndroidRuntime(8513): at android.widget.ExpandableListConnector.getView(ExpandableListConnector.java:451)
03-23 20:21:15.927: E/AndroidRuntime(8513): at android.widget.AbsListView.obtainView(AbsListView.java:2177)
03-23 20:21:15.927: E/AndroidRuntime(8513): at android.widget.ListView.makeAndAddView(ListView.java:1840)
03-23 20:21:15.927: E/AndroidRuntime(8513): at android.widget.ListView.fillDown(ListView.java:675)
03-23 20:21:15.927: E/AndroidRuntime(8513): at android.widget.ListView.fillSpecific(ListView.java:1333)
03-23 20:21:15.927: E/AndroidRuntime(8513): at android.widget.ListView.layoutChildren(ListView.java:1633)
03-23 20:21:15.927: E/AndroidRuntime(8513): at android.widget.AbsListView.onLayout(AbsListView.java:2012)
03-23 20:21:15.927: E/AndroidRuntime(8513): at android.view.View.layout(View.java:14289)
03-23 20:21:15.927: E/AndroidRuntime(8513): at android.view.ViewGroup.layout(ViewGroup.java:4562)
03-23 20:21:15.927: E/AndroidRuntime(8513): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1076)
03-23 20:21:15.927: E/AndroidRuntime(8513): at android.view.View.layout(View.java:14289)
03-23 20:21:15.927: E/AndroidRuntime(8513): at android.view.ViewGroup.layout(ViewGroup.java:4562)
03-23 20:21:15.927: E/AndroidRuntime(8513): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
03-23 20:21:15.927: E/AndroidRuntime(8513): at android.view.View.layout(View.java:14289)
03-23 20:21:15.927: E/AndroidRuntime(8513): at android.view.ViewGroup.layout(ViewGroup.java:4562)
03-23 20:21:15.927: E/AndroidRuntime(8513): at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:349)
03-23 20:21:15.927: E/AndroidRuntime(8513): at android.view.View.layout(View.java:14289)
03-23 20:21:15.927: E/AndroidRuntime(8513): at android.view.ViewGroup.layout(ViewGroup.java:4562)
03-23 20:21:15.927: E/AndroidRuntime(8513): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
03-23 20:21:15.927: E/AndroidRuntime(8513): at android.view.View.layout(View.java:14289)
03-23 20:21:15.927: E/AndroidRuntime(8513): at android.view.ViewGroup.layout(ViewGroup.java:4562)
03-23 20:21:15.927: E/AndroidRuntime(8513): at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1976)
03-23 20:21:15.927: E/AndroidRuntime(8513): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1730)
03-23 20:21:15.927: E/AndroidRuntime(8513): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
03-23 20:21:15.927: E/AndroidRuntime(8513): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
03-23 20:21:15.927: E/AndroidRuntime(8513): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
03-23 20:21:15.927: E/AndroidRuntime(8513): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
03-23 20:21:15.927: E/AndroidRuntime(8513): at android.view.Choreographer.doFrame(Choreographer.java:532)
03-23 20:21:15.927: E/AndroidRuntime(8513): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
03-23 20:21:15.927: E/AndroidRuntime(8513): at android.os.Handler.handleCallback(Handler.java:730)
03-23 20:21:15.927: E/AndroidRuntime(8513): at android.os.Handler.dispatchMessage(Handler.java:92)
03-23 20:21:15.927: E/AndroidRuntime(8513): at android.os.Looper.loop(Looper.java:137)
03-23 20:21:15.927: E/AndroidRuntime(8513): at android.app.ActivityThread.main(ActivityThread.java:5103)
03-23 20:21:15.927: E/AndroidRuntime(8513): at java.lang.reflect.Method.invokeNative(Native Method)
03-23 20:21:15.927: E/AndroidRuntime(8513): at java.lang.reflect.Method.invoke(Method.java:525)
03-23 20:21:15.927: E/AndroidRuntime(8513): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
03-23 20:21:15.927: E/AndroidRuntime(8513): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-23 20:21:15.927: E/AndroidRuntime(8513): at dalvik.system.NativeStart.main(Native Method)
package com.devleb.expandablelistdemo3;
import android.app.Activity;
import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
public class ExpandableAdapter extends BaseExpandableListAdapter implements ExpandableListAdapter {
public Context context;
private LayoutInflater vi;
private String[][] data;
int _objInt;
private static final int GROUP_ITEM_RESOURCE = R.layout.group_layout;
private static final int CHILD_ITEM_RESOURCE = R.layout.child_layout;
public ExpandableAdapter(Context context, Activity activity, String[][] data) {
this.data = data;
this.context = context;
vi = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
_objInt = data.length;
}
@Override
public String getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return data[groupPosition][childPosition];
}
@Override
public long getChildId(int arg0, int arg1) {
// TODO Auto-generated method stub
return arg1;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View v, ViewGroup parent) {
// TODO Auto-generated method stub
String child = getChild(groupPosition, childPosition);
int id_res = 0;
if (groupPosition == 0) {
if (childPosition == 0)
id_res = R.drawable.algeria_flag;
if (childPosition == 1)
id_res = R.drawable.cote_divoire_flag;
if (childPosition == 3)
id_res = R.drawable.nigeria_flag;
if (childPosition == 4)
id_res = R.drawable.cameroon_flag;
if (childPosition == 5)
id_res = R.drawable.ghana_flag;
} else if (groupPosition == 1) {
if (childPosition == 0)
id_res = R.drawable.australia;
if (childPosition == 1)
id_res = R.drawable.japan;
if (childPosition == 2)
id_res = R.drawable.iran_flag;
if (childPosition == 3)
id_res = R.drawable.korea_flag;
} else if (groupPosition == 2) {
if (childPosition == 0)
id_res = R.drawable.belgium_flag;
if (childPosition == 1)
id_res = R.drawable.croatian_flag;
if (childPosition == 2)
id_res = R.drawable.france_flag;
if (childPosition == 3)
id_res = R.drawable.gress;
if (childPosition == 4)
id_res = R.drawable.netherlands_flag;
if (childPosition == 5)
id_res = R.drawable.russia_flag;
if (childPosition == 6)
id_res = R.drawable.switzerland;
if (childPosition == 7)
id_res = R.drawable.bousna;
if (childPosition == 8)
id_res = R.drawable.england_flag;
if (childPosition == 9)
id_res = R.drawable.germany_flag;
if (childPosition == 10)
id_res = R.drawable.italy_flag;
if (childPosition == 11)
id_res = R.drawable.portugal;
if (childPosition == 12)
id_res = R.drawable.spain;
} else if (groupPosition == 3) {
if (childPosition == 0)
id_res = R.drawable.costa_rica_flag;
if (childPosition == 1)
id_res = R.drawable.mexico_flag;
if (childPosition == 2)
id_res = R.drawable.honduras_flag;
if (childPosition == 3)
id_res = R.drawable.united_states_flag;
} else if (groupPosition == 4) {
if (childPosition == 0)
id_res = R.drawable.argentina_flag;
if (childPosition == 1)
id_res = R.drawable.czech_republic_flag;
if (childPosition == 2)
id_res = R.drawable.ecuador_flag;
if (childPosition == 3)
id_res = R.drawable.brazil_flag;
if (childPosition == 4)
id_res = R.drawable.colombia_flag;
if (childPosition == 5)
id_res = R.drawable.uruguay_flag;
}
if (child != null) {
v = vi.inflate(CHILD_ITEM_RESOURCE, null);
ViewHolder holder = new ViewHolder(v);
//holder.text.setText(Html.fromHtml(child));
holder.text.setText(child);
holder.imageview.setImageResource(id_res);
}
return v;
}
@Override
public int getChildrenCount(int arg0) {
// TODO Auto-generated method stub
return data[arg0].length;
}
@Override
public Object getGroup(int arg0) {
// TODO Auto-generated method stub
return "group: " + arg0;
}
@Override
public int getGroupCount() {
// TODO Auto-generated method stub
return data.length;
}
@Override
public long getGroupId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public View getGroupView(int groupPosition, boolean arg1, View v,
ViewGroup arg3) {
// TODO Auto-generated method stub
String group = null;
// int id_res = 0;
long group_id = getGroupId(groupPosition);
if (group_id == 0) {
group = "Africa";
// id_res = R.drawable.audi;
} else if (group_id == 1) {
group = "Asia";
// id_res = R.drawable.bmw;
} else if (group_id == 2) {
group = "Europe";
// id_res = R.drawable.ferrari;
} else if (group_id == 3) {
group = "North and Central America";
} else if (group_id == 4) {
group = "South America";
}
if (group != null) {
v = vi.inflate(GROUP_ITEM_RESOURCE, null);
ViewHolder holder = new ViewHolder(v);
holder.text.setText(Html.fromHtml(group));
// holder.imageview.setImageResource(id_res);
}
return v;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return true;
}
@Override
public boolean isChildSelectable(int arg0, int arg1) {
// TODO Auto-generated method stub
return true;
}
}
package com.devleb.expandablelistdemo3;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.widget.ExpandableListView;
public class MainActivity extends Activity {
final Context context = this;
private static final String[][] data = {
{ "Alegria", " Côte d'Ivoire", " Nigeria", "Cameroon", "Ghana" },
{ "Australia", " Japan", "Iran", "Korea" },
{ "Belgium", " Croatia", " France", "Greece", " Netherlands",
" Russia", " Switzerland", "Bosnia and Herzegovina",
" England", " Germany", "Italy", "Portugal", "Spain" },
{ "Costa Rica", " Mexico", "Honduras", " USA" },
{ "Argentina", " Chile", "Ecuador", " Brazil", "Colombia",
"Uruguay" } };
private ExpandableListView expandableListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
expandableListView = (ExpandableListView) findViewById(R.id.listView);
expandableListView
.setAdapter(new ExpandableAdapter(context, this, data));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
package com.devleb.expandablelistdemo3;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
public class ViewHolder {
public TextView text;
public ImageView imageview;
public ViewHolder(View v) {
this.text = (TextView) v.findViewById(R.id.text1);
this.imageview = (ImageView) v.findViewById(R.id.image1);
}
}
答案 0 :(得分:0)
问题似乎与未设置ViewHolder的文本字段有关。
从堆栈跟踪中观察它在ExpandableAdapter.java:129上给出了NullPointerException。正如您在ViewHolder的构造函数中正确设置字段一样,在我看来,您使用的布局资源实际上并不包含标识text1
,因此FindViewById(R.id.text1)
返回null。 / p>