我正在尝试在片段中创建一个快餐栏。我尝试了很多其他类似的问题,但无法找到解决方案。当片段打开时,应用程序崩溃。我不知道问题是什么。有人能帮我吗。提前致谢。 这是我的片段:
public class MainFragment extends Fragment {
public MainFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main, null);
ImageView test = new ImageView(getContext());
ImageView nyimg = (ImageView)view.findViewById(R.id.glidimg);
Glide.with(getContext()).load("http://inthecheesefactory.com/uploads/source/glidepicasso/cover.jpg").into(nyimg);
Snackbar.make(view, "Some Message", Snackbar.LENGTH_INDEFINITE).show();
// Picasso.with(getContext()).load("http://inthecheesefactory.com/uploads/source/glidepicasso/cover.jpg").into(test);
return view;
//return inflater.inflate(R.layout.fragment_main, container, false);
}
}
这是我的xml文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context="com.example.imran.myapp.MainFragment"
>
<TextView
android:id="@+id/main_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="35sp"
android:textColor="@color/white"
android:textAlignment="center"
android:background="@drawable/title_background"
android:layout_gravity="center"
android:text="Welcome" />
<LinearLayout android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_marginTop="40dp"
android:textSize="25sp"
android:textStyle="bold"
android:textAlignment="center"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="You are logged in."
/>
<ImageView
android:id="@+id/glidimg"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:contentDescription="my image"
android:src="@drawable/ic_menu_gallery"
/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="www.mirzaphotos.com"
android:layout_gravity="bottom"
android:textAlignment="center"
android:textSize="15sp"
android:textStyle="bold"
android:textColor="@color/darkorange"
android:layout_alignParentBottom="true"
/>
</RelativeLayout>
我收到此错误:
04-08 17:57:18.559 4049-4049/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.imran.myapp, PID: 4049
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.imran.myapp/com.example.imran.myapp.Home}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.view.ViewGroup.getContext()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2808)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2873)
at android.app.ActivityThread.access$900(ActivityThread.java:181)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1482)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6145)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.view.ViewGroup.getContext()' on a null object reference
at android.support.design.widget.Snackbar.<init>(Snackbar.java:180)
at android.support.design.widget.Snackbar.make(Snackbar.java:206)
at com.example.imran.myapp.MainFragment.onCreateView(MainFragment.java:42)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1026)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1207)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1572)
at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:330)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:511)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1234)
at android.app.Activity.performStart(Activity.java:6412)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2771)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2873)
at android.app.ActivityThread.access$900(ActivityThread.java:181)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1482)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6145)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
04-08 17:57:18.559 4068-4097/? D/AcmsCertificateMngr: handleAppRemoved() Enter com.example.imran.myapp
04-08 17:57:18.569 1036-1514/? V/ApplicationPolicy: isApplicationStateBlocked userId 0 pkgname com.example.imran.myapp
04-08 17:57:18.579 1036-1514/? W/ActivityManager: Force finishing activity com.example.imran.myapp/.Home
答案 0 :(得分:2)
当View未附加到父级时,它无法创建Snackbar(它从getContext返回null)。将您的Snackbar.create()
移至onActivityCreated
或其他方法,如onResume()
等生命周期。
public class MainFragment extends Fragment {
View view;
public MainFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_main, null);
ImageView test = new ImageView(getContext());
ImageView nyimg = (ImageView)view.findViewById(R.id.glidimg);
Glide.with(getContext()).load("http://inthecheesefactory.com/uploads/source/glidepicasso/cover.jpg").into(nyimg);
// Picasso.with(getContext()).load("http://inthecheesefactory.com/uploads/source/glidepicasso/cover.jpg").into(test);
return view;
//return inflater.inflate(R.layout.fragment_main, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Snackbar.make(view, "Some Message", Snackbar.LENGTH_INDEFINITE).show();
}
}
答案 1 :(得分:1)
请尝试使用CoordinatorLayout。
Snackbar将向上查看树,试图找到合适的父级,该父级被定义为CoordinatorLayout或窗口装饰的内容视图,以先到者为准。
http://www.androidhive.info/2015/09/android-material-design-snackbar-example/