我的ActivityA有一个包含一个按钮的水平幻灯片菜单。 这是行为:
1)在onClick期间,它将通过startActivityForResult()调用ActivityB,供用户选择类别组。
2)然后,它将调用另一个活动,ActivityC供用户选择一个子类别。
3)之后,它将返回ActivityA。 (ActivityB和ActivityC在完成后调用finish()。)
在恢复到ActivityA之后,应用程序崩溃了,这就是我在Logcat中看到的内容。他们都没有指向我的代码。有什么建议吗?
**而且,我发现NullPointer发生在CustomHorizontalScrollView.onLayout()中 那是因为滚动菜单在恢复后失去焦点吗?或者,它与哪个TargetFragment应该显示混淆?我尝试在onLayout()中为该NullPointer添加try-catch,以便我的应用程序不会FC。滚动菜单和主要布局显示得很好。但只有搜索结果没有显示出来。
活动A:源代码
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LayoutInflater inflater = LayoutInflater.from(this);
scrollView = (CustomHorizontalScrollView) inflater.inflate(R.layout.slide_menu, null);
setContentView(scrollView);
menu = inflater.inflate(R.layout.horz_scroll_menu, null);
app = inflater.inflate(R.layout.main_list, null);
btnSlide = (ImageView) app.findViewById(R.id.ib_slide_menu);
btnSlide.setOnClickListener(new ClickListenerForScrolling(scrollView, menu));
fAdsSearch = (AdsSearchFragment)getSupportFragmentManager().findFragmentById(R.id.f_search);
fActionbar = (ActionBarFragment) getSupportFragmentManager().findFragmentById(R.id.f_actionbar);
fAdsSearch.setTargetFragment(getSupportFragmentManager().findFragmentById(R.id.f_main), 0);
scrollView.initViews(new View[] {menu, app}, 1, new SizeCallbackForMenu(btnSlide));
}
slide_menu.xml:源代码
<?xml version="1.0" encoding="utf-8"?>
<CustomHorizontalScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#00ffffff"
android:padding="0px"
android:layout_margin="0px"
android:fadingEdge="none"
android:fadingEdgeLength="0px"
android:scrollbars="none">
<LinearLayout
android:id="@+id/top"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="horizontal"
android:background="#ffffffff"
android:padding="0px"
android:layout_margin="0px">
</LinearLayout>
</CustomHorizontalScrollView>
horz_scroll_menu.xml:源代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<fragment
android:id="@+id/f_filter_menu"
android:name="MenuFragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</fragment>
</LinearLayout>
main_list.xml:源代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<fragment
android:id="@+id/f_actionbar"
android:name="ActionBarFragment"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<fragment
android:id="@+id/f_search"
android:name="SearchFragment"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<fragment
android:id="@+id/f_main"
android:name="MainFragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
Logcat错误:
02-08 15:57:12.959: E/AndroidRuntime(19322): FATAL EXCEPTION: main
02-08 15:57:12.959: E/AndroidRuntime(19322): java.lang.NullPointerException
02-08 15:57:12.959: E/AndroidRuntime(19322): at android.widget.Spinner.makeAndAddView(Spinner.java)
02-08 15:57:12.959: E/AndroidRuntime(19322): at android.widget.Spinner.layout(Spinner.java)
02-08 15:57:12.959: E/AndroidRuntime(19322): at android.widget.Spinner.onLayout(Spinner.java)
02-08 15:57:12.959: E/AndroidRuntime(19322): at android.view.View.layout(View.java)
02-08 15:57:12.959: E/AndroidRuntime(19322): at android.view.ViewGroup.layout(ViewGroup.java)
02-08 15:57:12.959: E/AndroidRuntime(19322): at android.widget.LinearLayout.setChildFrame(LinearLayout.java)
02-08 15:57:12.959: E/AndroidRuntime(19322): at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java)
02-08 15:57:12.959: E/AndroidRuntime(19322): at android.widget.LinearLayout.onLayout(LinearLayout.java)
02-08 15:57:12.959: E/AndroidRuntime(19322): at android.view.View.layout(View.java)
02-08 15:57:12.959: E/AndroidRuntime(19322): at android.view.ViewGroup.layout(ViewGroup.java)
02-08 15:57:12.959: E/AndroidRuntime(19322): at android.widget.LinearLayout.setChildFrame(LinearLayout.java)
02-08 15:57:12.959: E/AndroidRuntime(19322): at android.widget.LinearLayout.layoutVertical(LinearLayout.java)
02-08 15:57:12.959: E/AndroidRuntime(19322): at android.widget.LinearLayout.onLayout(LinearLayout.java)
02-08 15:57:12.959: E/AndroidRuntime(19322): at android.view.View.layout(View.java)
02-08 15:57:12.959: E/AndroidRuntime(19322): at android.view.ViewGroup.layout(ViewGroup.java)
02-08 15:57:12.959: E/AndroidRuntime(19322): at android.widget.FrameLayout.onLayout(FrameLayout.java)
02-08 15:57:12.959: E/AndroidRuntime(19322): at android.view.View.layout(View.java)
02-08 15:57:12.959: E/AndroidRuntime(19322): at android.view.ViewGroup.layout(ViewGroup.java)
02-08 15:57:12.959: E/AndroidRuntime(19322): at android.widget.LinearLayout.setChildFrame(LinearLayout.java)
02-08 15:57:12.959: E/AndroidRuntime(19322): at android.widget.LinearLayout.layoutVertical(LinearLayout.java)
02-08 15:57:12.959: E/AndroidRuntime(19322): at android.widget.LinearLayout.onLayout(LinearLayout.java)
02-08 15:57:12.959: E/AndroidRuntime(19322): at android.view.View.layout(View.java)
02-08 15:57:12.959: E/AndroidRuntime(19322): at android.view.ViewGroup.layout(ViewGroup.java)
02-08 15:57:12.959: E/AndroidRuntime(19322): at android.widget.LinearLayout.setChildFrame(LinearLayout.java)
02-08 15:57:12.959: E/AndroidRuntime(19322): at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java)
02-08 15:57:12.959: E/AndroidRuntime(19322): at android.widget.LinearLayout.onLayout(LinearLayout.java)
02-08 15:57:12.959: E/AndroidRuntime(19322): at android.view.View.layout(View.java)
02-08 15:57:12.959: E/AndroidRuntime(19322): at android.view.ViewGroup.layout(ViewGroup.java)
02-08 15:57:12.959: E/AndroidRuntime(19322): at android.widget.FrameLayout.onLayout(FrameLayout.java)
02-08 15:57:12.959: E/AndroidRuntime(19322): at android.widget.HorizontalScrollView.onLayout(HorizontalScrollView.java)
02-08 15:57:12.959: E/AndroidRuntime(19322): at android.view.View.layout(View.java)
02-08 15:57:12.959: E/AndroidRuntime(19322): at android.view.ViewGroup.layout(ViewGroup.java)
02-08 15:57:12.959: E/AndroidRuntime(19322): at android.widget.FrameLayout.onLayout(FrameLayout.java)
02-08 15:57:12.959: E/AndroidRuntime(19322): at android.view.View.layout(View.java)
02-08 15:57:12.959: E/AndroidRuntime(19322): at android.view.ViewGroup.layout(ViewGroup.java)
02-08 15:57:12.959: E/AndroidRuntime(19322): at android.widget.LinearLayout.setChildFrame(LinearLayout.java)
02-08 15:57:12.959: E/AndroidRuntime(19322): at android.widget.LinearLayout.layoutVertical(LinearLayout.java)
02-08 15:57:12.959: E/AndroidRuntime(19322): at android.widget.LinearLayout.onLayout(LinearLayout.java)
02-08 15:57:12.959: E/AndroidRuntime(19322): at android.view.View.layout(View.java)
02-08 15:57:12.959: E/AndroidRuntime(19322): at android.view.ViewGroup.layout(ViewGroup.java)
02-08 15:57:12.959: E/AndroidRuntime(19322): at android.widget.FrameLayout.onLayout(FrameLayout.java)
02-08 15:57:12.959: E/AndroidRuntime(19322): at android.view.View.layout(View.java)
02-08 15:57:12.959: E/AndroidRuntime(19322): at android.view.ViewGroup.layout(ViewGroup.java)
02-08 15:57:12.959: E/AndroidRuntime(19322): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java)
02-08 15:57:12.959: E/AndroidRuntime(19322): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java)
02-08 15:57:12.959: E/AndroidRuntime(19322): at android.os.Handler.dispatchMessage(Handler.java)
02-08 15:57:12.959: E/AndroidRuntime(19322): at android.os.Looper.loop(Looper.java)
02-08 15:57:12.959: E/AndroidRuntime(19322): at android.app.ActivityThread.main(ActivityThread.java)
02-08 15:57:12.959: E/AndroidRuntime(19322): at java.lang.reflect.Method.invokeNative(Native Method)
02-08 15:57:12.959: E/AndroidRuntime(19322): at java.lang.reflect.Method.invoke(Method.java)
02-08 15:57:12.959: E/AndroidRuntime(19322): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
02-08 15:57:12.959: E/AndroidRuntime(19322): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
02-08 15:57:12.959: E/AndroidRuntime(19322): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
事实证明,这个NullPointerException与幻灯片菜单无关。这是因为我通过将适配器设置为null来清理列表数据。谢谢你的帮助。