1. In the fragment, the code is as follows. It is necessary that when you click on the button1 , the dialog opens to the link to the library on Git hub

fragment code

  public class velstudent_Fragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_secret, null); TextView textView = (TextView) v.findViewById(R.id.textView1); textView.setText(Html.fromHtml(getResources().getString(R.string.info))); Button button1 = (Button) v.findViewById(R.id.button); button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { new MaterialDialog.Builder(getActivity()) .title(R.string.app_name) .content(R.string.infoprivichki) .positiveText(R.string.drawer_close) .negativeText(R.string.drawer_open) .show(); } }); return v; } 

}

when switching to this fragment, it gives an error:

  10-27 13:33:53.264 30255-30255/com.whitestar.successstudent E/AndroidRuntime: FATAL EXCEPTION: main 10-27 13:33:53.264 30255-30255/com.whitestar.successstudent E/AndroidRuntime: java.lang.NullPointerException 10-27 13:33:53.264 30255-30255/com.whitestar.successstudent E/AndroidRuntime: at com.whitestar.successstudent.fragments.velstudent_Fragment.onCreateView(velstudent_Fragment.java:44) 10-27 13:33:53.264 30255-30255/com.whitestar.successstudent E/AndroidRuntime: at android.support.v4.app.Fragment.performCreateView(Fragment.java:1965) 10-27 13:33:53.264 30255-30255/com.whitestar.successstudent E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1078) 10-27 13:33:53.264 30255-30255/com.whitestar.successstudent E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1259) 10-27 13:33:53.264 30255-30255/com.whitestar.successstudent E/AndroidRuntime: at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738) 10-27 13:33:53.264 30255-30255/com.whitestar.successstudent E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1624) 10-27 13:33:53.264 30255-30255/com.whitestar.successstudent E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517) 10-27 13:33:53.264 30255-30255/com.whitestar.successstudent E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:615) 10-27 13:33:53.264 30255-30255/com.whitestar.successstudent E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:92) 10-27 13:33:53.264 30255-30255/com.whitestar.successstudent E/AndroidRuntime: at android.os.Looper.loop(Looper.java:137) 10-27 13:33:53.264 30255-30255/com.whitestar.successstudent E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:4745) 10-27 13:33:53.264 30255-30255/com.whitestar.successstudent E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method) 10-27 13:33:53.264 30255-30255/com.whitestar.successstudent E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:511) 10-27 13:33:53.264 30255-30255/com.whitestar.successstudent E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 10-27 13:33:53.264 30255-30255/com.whitestar.successstudent E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 10-27 13:33:53.264 30255-30255/com.whitestar.successstudent E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method) 10-27 13:38:53.340 30255-30255/? I/Process: Sending signal. PID: 30255 SIG: 9 

fragment code

  <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="16dp"> <ScrollView android:id="@+id/mScroll2" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:onClick="test1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="Medium Text" android:id="@+id/textView1" /> </ScrollView> <Button // изменил буттон на етот android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="New Button" android:id="@+id/button" android:layout_alignTop="@+id/mScroll2" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /> </RelativeLayout> 

    2 answers 2

    Try this:

     public class velstudent_Fragment extends Fragment { Button button1; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_secret, null); TextView textView = (TextView) v.findViewById(R.id.textView1); textView.setText(Html.fromHtml(getResources().getString(R.string.info))); button1 = (Button) v.findViewById(R.id.button); return v; } @Override public void onResume() { button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { new MaterialDialog.Builder(getActivity()) .title(R.string.app_name) .content(R.string.infoprivichki) .positiveText(R.string.drawer_close) .negativeText(R.string.drawer_open) .show(); } }); } } 

    The idea is to get the Context (i.e. getActivity ()) in onResume () when the fragment is already accurately displayed.

      That's because you didn’t see what type of parameter should be passed to the new MaterialDialog.Builder(...) method. There parameter of type Context . In the Fragment Context can be obtained using, for example, getActivity() . That is, you get a new MaterialDialog.Builder(getActivity()) instead of a new MaterialDialog.Builder(this)


      About the error:

       <Button android:id="@+id/button" android:layout_width="@dimen/fab_big" android:layout_height="@dimen/fab_big" android:background="@drawable/fab"/> 

      Here you have an object of type Button
      Further in the code:

       ImageButton button1 = (ImageButton) v.findViewById(R.id.button); 

      And here you are trying to find (findViewById) an object of type ImageButton . If you specified Button in the markup, then look for Button . Replace

       ImageButton button1 = (ImageButton) v.findViewById(R.id.button); 

      on

       Button button1 = (Button) v.findViewById(R.id.button); 
      • yes it did, but ... when launched in the on-one as soon as the emulator opens the application, the dialog pops up ... + another error when switching to this fragment throws out an error now above the code @metalurgus - Andrey
      • @Andrew, Why the dialogue immediately pops up - see for yourself. About the error that is on the 44th line: velstudent_Fragment.java:44 - Vladyslav Matviienko
      • I figured out the problem at startup ... But when I switched to this fragment in an impeller, I get an error above, I laid out the code + laid out the light - release code - Andrei
      • @ Andrei, read the comment completely. What is on line 44 in the velstudent_Fragment.java file? - Vladyslav Matviienko
      • 2 times reread @metalurgus something I do not understand? Yes, the error is in line 44 - Andrei