我的 ActivityA 有一个包含一个按钮的水平滑动菜单。这是行为:
1)在onClick期间,它会通过startActivityForResult()调用ActivityB,供用户选择类别组。
2)然后,它会调用另一个活动,ActivityC,供用户选择一个子类别。
3)之后,它将返回到ActivityA。(ActivityB 和 ActivityC 都在完成后调用 finish()。)
在恢复到 ActivityA 后,应用程序崩溃了,这是我在 Logcat 中看到的。他们都没有指向我的代码。请问有什么建议吗?
** 而且,我刚刚发现 NullPointer 发生在 CustomHorizontalScrollView.onLayout() 是因为滚动菜单在恢复后失去焦点吗?或者,它与应该显示哪个 TargetFragment 混淆了?我尝试在 onLayout() 中为 NullPointer 放入 try-catch,这样我的应用程序就不会 FC。滚动菜单和主布局显示得很好。但只有搜索结果没有显示。
活动一:源代码
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>
日志错误:
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)