Found the code in the forum, which seems to be all work. After clicking in the console, Exception is displayed. Tell me how to properly handle the button.

In the manifest prescribed:

<uses-permission android:name="android.permission.CALL_PHONE"/> 

In the adapter:

 @Override public void onBindViewHolder(final ViewHolder holder, final int position) { holder.call.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { fragmentWelcom.call(position); } }); 

In the fragment:

 public void call(final int position) { try { int number = 23; Intent intent = new Intent(Intent.ACTION_DIAL); intent.setData(Uri.parse("tel:" + number); startActivity(intent); } catch (Exception e) { e.printStackTrace(); } } 

Stacktrace:

 W/System.err: java.lang.NullPointerException W/System.err: at com.cholak.v3.appPineapple.fragments.FragmentWelcom.call(FragmentWelcom.java:197) W/System.err: at com.cholak.v3.appPineapple.adapter.RecyclerAdapter$3.onClick(RecyclerAdapter.java:85) W/System.err: at android.view.View.performClick(View.java:4295) W/System.err: at android.view.View$PerformClick.run(View.java:17456) W/System.err: at android.os.Handler.handleCallback(Handler.java:615) W/System.err: at android.os.Handler.dispatchMessage(Handler.java:92) W/System.err: at android.os.Looper.loop(Looper.java:174) W/System.err: at android.app.ActivityThread.main(ActivityThread.java:4952) W/System.err: at java.lang.reflect.Method.invokeNative(Native Method) W/System.err: at java.lang.reflect.Method.invoke(Method.java:511) W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027) W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794) W/System.err: at dalvik.system.NativeStart.main(Native Method) 
  • in catch add the line e.printStackTrace(); and attach the stacktrace to the question - tCode
  • Well, respectively, this value is contactsList.get(position).getPrice() you have a null looking for an error - tCode
  • Maybe this is not essential (I have not tested it), but in the code that I use, the tel:/ pattern is used. If after correction NullPointerException still will not work - try to change the pattern - zTrap
  • I made an int number variable, and wrote a number there, instead of contactsList.get (position) .getPrice () Now the same error in the line 'startActivity (intent); ' - Fedia

1 answer 1

For the adequate startActivity() method to work, it must be called from the Activity , not from the Fragment . Replace startActivity(intent) with getActivity().startActivity(intent)

  • did not help, the error is the same. - Fedia
  • Can the whole listing error? - zTrap
  • @Fedia Are you sure that a fragment instance ( fragmentWelcom variable) in the adapter is not null ? - zTrap
  • Yes ( fragmentWelcom variable) has been initialized. ( fragmentWelcom = new FragmentWelcom(); ) - Fedia
  • Are you here ? Will you help me ? - Fedia