我有expandablelistview,每个父级都有1个子布局,当我将onClickListener分配给childView内的ImageView时,应用程序运行但崩溃时片段打开,其中imageView是
代码:
public class FragmentA extends Fragment{
private List<Category> catList;
ExpandableListView exList;
ImageView del,info,collapse;
ExpandableAdapter exAdpt;
public FragmentA() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_a, null);
exList = (ExpandableListView) v.findViewById(R.id.expandablelist);
//exList.setIndicatorBounds(5, 5);
//exList.setIndicatorBounds(0, 20);
//exList.setDivider(null);
exList.setDividerHeight(3);
exList.setGroupIndicator(null);
del = (ImageView) v.findViewById(R.id.del_icon);
info = (ImageView) v.findViewById(R.id.inf_icon);
collapse = (ImageView) v.findViewById(R.id.collapse_icon);
collapse.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
return v;
}
错误logcat:
07-28 12:15:13.937: E/art(23655): Unrecognized option -XX:mainThreadStackSize=24K
07-28 12:15:14.007: E/cutils-trace(23655): Error opening trace file: No such file or directory (2)
07-28 12:15:14.728: E/art(23669): Unrecognized option -XX:mainThreadStackSize=24K
07-28 12:15:14.798: E/cutils-trace(23669): Error opening trace file: No such file or directory (2)
07-28 12:15:16.690: E/AndroidRuntime(23685): FATAL EXCEPTION: main
07-28 12:15:16.690: E/AndroidRuntime(23685): Process: com.example.androidhive, PID: 23685
07-28 12:15:16.690: E/AndroidRuntime(23685): java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
07-28 12:15:16.690: E/AndroidRuntime(23685): at com.example.androidhive.FragmentA.onCreateView(FragmentA.java:48)
07-28 12:15:16.690: E/AndroidRuntime(23685): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
07-28 12:15:16.690: E/AndroidRuntime(23685): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
07-28 12:15:16.690: E/AndroidRuntime(23685): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
07-28 12:15:16.690: E/AndroidRuntime(23685): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
07-28 12:15:16.690: E/AndroidRuntime(23685): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
07-28 12:15:16.690: E/AndroidRuntime(23685): at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
07-28 12:15:16.690: E/AndroidRuntime(23685): at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
07-28 12:15:16.690: E/AndroidRuntime(23685): at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
07-28 12:15:16.690: E/AndroidRuntime(23685): at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
07-28 12:15:16.690: E/AndroidRuntime(23685): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
07-28 12:15:16.690: E/AndroidRuntime(23685): at android.view.View.measure(View.java:16497)
07-28 12:15:16.690: E/AndroidRuntime(23685): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
07-28 12:15:16.690: E/AndroidRuntime(23685): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
07-28 12:15:16.690: E/AndroidRuntime(23685): at android.view.View.measure(View.java:16497)
07-28 12:15:16.690: E/AndroidRuntime(23685): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
07-28 12:15:16.690: E/AndroidRuntime(23685): at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
07-28 12:15:16.690: E/AndroidRuntime(23685): at android.view.View.measure(View.java:16497)
07-28 12:15:16.690: E/AndroidRuntime(23685): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
07-28 12:15:16.690: E/AndroidRuntime(23685): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
07-28 12:15:16.690: E/AndroidRuntime(23685): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
07-28 12:15:16.690: E/AndroidRuntime(23685): at android.view.View.measure(View.java:16497)
07-28 12:15:16.690: E/AndroidRuntime(23685): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1916)
07-28 12:15:16.690: E/AndroidRuntime(23685): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1113)
07-28 12:15:16.690: E/AndroidRuntime(23685): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1295)
07-28 12:15:16.690: E/AndroidRuntime(23685): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
07-28 12:15:16.690: E/AndroidRuntime(23685): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
07-28 12:15:16.690: E/AndroidRuntime(23685): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
07-28 12:15:16.690: E/AndroidRuntime(23685): at android.view.Choreographer.doCallbacks(Choreographer.java:574)
07-28 12:15:16.690: E/AndroidRuntime(23685): at android.view.Choreographer.doFrame(Choreographer.java:544)
07-28 12:15:16.690: E/AndroidRuntime(23685): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
07-28 12:15:16.690: E/AndroidRuntime(23685): at android.os.Handler.handleCallback(Handler.java:733)
07-28 12:15:16.690: E/AndroidRuntime(23685): at android.os.Handler.dispatchMessage(Handler.java:95)
07-28 12:15:16.690: E/AndroidRuntime(23685): at android.os.Looper.loop(Looper.java:136)
07-28 12:15:16.690: E/AndroidRuntime(23685): at android.app.ActivityThread.main(ActivityThread.java:5017)
07-28 12:15:16.690: E/AndroidRuntime(23685): at java.lang.reflect.Method.invoke(Native Method)
07-28 12:15:16.690: E/AndroidRuntime(23685): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-28 12:15:16.690: E/AndroidRuntime(23685): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
适配器:
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater inflater = (LayoutInflater)ctx.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.item_layout, parent, false);
}
TextView itemName = (TextView) v.findViewById(R.id.itemName);
TextView itemDescr = (TextView) v.findViewById(R.id.itemDescr);
ImageView itemImg = (ImageView) v.findViewById(R.id.itemImg);
ImageView itemBarcode = (ImageView) v.findViewById(R.id.itemBarcode);
ImageView collapse = (ImageView) v.findViewById(R.id.collapse_icon);
ItemDetail det = catList.get(groupPosition).getItemList().get(childPosition);
itemName.setText(det.getName());
itemDescr.setText(det.getDescr());
itemImg.setImageResource(det.getImgId());
itemBarcode.setImageResource(det.getBarImg());
return v;
}
答案 0 :(得分:0)
我在getChildView中找到了解决方案,将onClickListener分配给ExpandableListView适配器类中的imageView,并且它有效。
答案 1 :(得分:0)
您必须检查此视图,该视图必须存在于您正在膨胀的主视图中:
public class AccountService {
private static AccountDao accountinfo;
public AccountService() {
accountinfo = new AccountDao();
} }
您的logcat错误显示存在空指针异常:
07-28 12:15:16.690:E / AndroidRuntime(23685):java.lang.NullPointerException:尝试调用虚拟方法'void android.widget.ImageView.setOnClickListener(android.view.View $ OnClickListener)'on空对象引用