1
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;


/**
 * A simple {@link Fragment} subclass.
 */
public class send extends Fragment {

    private EditText mEditTextTo;
    private EditText mEditTextSubject;
    private EditText mEditTextMessage;


    public send() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_send, container, false);


        mEditTextTo = findViewById(R.id.edit_text_to);
        mEditTextSubject = findViewById(R.id.edit_text_subject); 
       //Cannot resolve method 'findViewById' in all find view by id 
        mEditTextMessage = findViewById(R.id.edit_text_message);

        Button buttonSend = findViewById(R.id.button_send);
        buttonSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendMail();
            }
        });
    }

    private void sendMail() {
        String recipientList = mEditTextTo.getText().toString();
        String[] recipients = recipientList.split(",");

        String subject = mEditTextSubject.getText().toString();
        String message = mEditTextMessage.getText().toString();

        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.putExtra(Intent.EXTRA_EMAIL, recipients);
        intent.putExtra(Intent.EXTRA_SUBJECT, subject);
        intent.putExtra(Intent.EXTRA_TEXT, message);

        intent.setType("message/rfc822");
        startActivity(Intent.createChooser(intent, "Choose an email client"));
    }
}

That is not working code any anyone me to write the exact code This is my XML of the send fragment................................

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".send">

    <!-- TODO: Update blank fragment layout -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">


        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="To:"
            android:textAppearance="@style/TextAppearance.AppCompat.Large" />

        <EditText
            android:id="@+id/edit_text_to"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textEmailAddress" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Subject:"
            android:textAppearance="@style/TextAppearance.AppCompat.Large" />

        <EditText
            android:id="@+id/edit_text_subject"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textEmailSubject" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Message:"
            android:textAppearance="@style/TextAppearance.AppCompat.Large" />

        <EditText
            android:id="@+id/edit_text_message"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="start|top"
            android:lines="10" />

        <Button
            android:id="@+id/button_send"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="send" />


    </LinearLayout>

Logcat Error when run the code

When I run the code it shows no errors but code did not run properly, what should I do now, can anyone help me to implement the send email form the navigation drawer. Or anyone adds the code of send fragment which is working properly and send mail pop up??????

     at android.app.ActivityThread.handleCreateService(ActivityThread.java:3404)
        at android.app.ActivityThread.-wrap4(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1683)
        at android.os.Handler.dispatchMessage(Handler.java:105)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6541)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
2019-09-24 11:59:42.995 4015-4015/? E/ActivityThread: Service com.google.android.rcs.service.service.JibeService has leaked IntentReceiver com.google.android.ims.network.e@dbb1e75 that was originally registered here. Are you missing a call to unregisterReceiver()?
    android.app.IntentReceiverLeaked: Service com.google.android.rcs.service.service.JibeService has leaked IntentReceiver com.google.android.ims.network.e@dbb1e75 that was originally registered here. Are you missing a call to unregisterReceiver()?
        at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:1333)
        at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:1114)
        at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1405)
        at android.app.ContextImpl.registerReceiver(ContextImpl.java:1378)
        at android.app.ContextImpl.registerReceiver(ContextImpl.java:1366)
        at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:603)
        at com.google.android.ims.network.a.<init>(SourceFile:48)
        at com.google.android.ims.f.a.a(SourceFile:12)
        at com.google.android.rcs.service.e.a(SourceFile:4)
        at com.google.android.rcs.service.service.JibeService.d(SourceFile:145)
        at com.google.android.rcs.service.service.JibeService.onCreate(SourceFile:91)
        at android.app.ActivityThread.handleCreateService(ActivityThread.java:3404)
        at android.app.ActivityThread.-wrap4(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1683)
        at android.os.Handler.dispatchMessage(Handler.java:105)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6541)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
2019-09-24 11:59:53.518 23707-23720/? E/memtrack: Couldn't load memtrack module
2019-09-24 12:00:00.022 23707-23720/? E/memtrack: Couldn't load memtrack module
2019-09-24 12:00:00.053 23707-23720/? E/memtrack: Couldn't load memtrack module
2019-09-24 12:00:00.146 23707-23720/? E/memtrack: Couldn't load memtrack module
2019-09-24 12:00:06.901 23707-23720/? E/memtrack: Couldn't load memtrack module
2019-09-24 12:00:06.947 23707-23720/? E/memtrack: Couldn't load memtrack module
2019-09-24 12:00:19.692 23707-23720/? E/memtrack: Couldn't load memtrack module
2019-09-24 12:00:32.128 23707-23724/? E/BatteryStatsService: modem info is invalid: ModemActivityInfo{ mTimestamp=0 mSleepTimeMs=0 mIdleTimeMs=0 mTxTimeMs[]=[0, 0, 0, 0, 0] mRxTimeMs=0 mEnergyUsed=0}
2019-09-24 12:00:46.157 28486-30420/? E/WakeLock: GCM_HB_ALARM release without a matched acquire!
2019-09-24 12:00:51.184 1396-1396/? E/hw-IPCThreadState: binder thread pool (1 threads) starved for 177 ms
2019-09-24 12:00:51.355 1396-1396/? E/hw-IPCThreadState: binder thread pool (1 threads) starved for 170 ms
2019-09-24 12:00:53.341 1396-1396/? E/hw-IPCThreadState: binder thread pool (1 threads) starved for 140 ms
2019-09-24 12:00:53.872 1396-1396/? E/hw-IPCThreadState: binder thread pool (1 threads) starved for 162 ms
2019-09-24 12:00:54.011 1396-1396/? E/hw-IPCThreadState: binder thread pool (1 threads) starved for 140 ms
2019-09-24 12:00:55.543 1396-1396/? E/hw-IPCThreadState: binder thread pool (1 threads) starved for 218 ms
2019-09-24 12:01:02.933 1396-1396/? E/hw-IPCThreadState: binder thread pool (1 threads) starved for 139 ms
2019-09-24 12:01:03.523 1405-1831/? E/SurfaceFlinger: ro.sf.lcd_density must be defined as a build property
2019-09-24 12:01:03.742 1396-1396/? E/hw-IPCThreadState: binder thread pool (1 threads) starved for 171 ms
2019-09-24 12:01:03.993 1396-1396/? E/hw-IPCThreadState: binder thread pool (1 threads) starved for 164 ms
2019-09-24 12:01:04.029 4851-4872/com.techneo360.pubguide11 E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008cdf
2019-09-24 12:01:04.029 4851-4872/com.techneo360.pubguide11 E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008cdf
2019-09-24 12:01:04.029 4851-4872/com.techneo360.pubguide11 E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008824
2019-09-24 12:01:04.030 4851-4872/com.techneo360.pubguide11 E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008824
2019-09-24 12:01:04.188 1396-1396/? E/hw-IPCThreadState: binder thread pool (1 threads) starved for 195 ms
2019-09-24 12:01:05.998 23707-23724/? E/BatteryStatsService: modem info is invalid: ModemActivityInfo{ mTimestamp=0 mSleepTimeMs=0 mIdleTimeMs=0 mTxTimeMs[]=[0, 0, 0, 0, 0] mRxTimeMs=0 mEnergyUsed=0}
2019-09-24 12:01:13.280 23707-23720/? E/memtrack: Couldn't load memtrack module
2019-09-24 12:01:33.353 23707-23720/? E/memtrack: Couldn't load memtrack module
2019-09-24 12:01:33.386 23707-23720/? E/memtrack: Couldn't load memtrack module
2019-09-24 12:01:48.554 23707-23720/? E/memtrack: Couldn't load memtrack module
2019-09-24 12:01:48.586 23707-23720/? E/memtrack: Couldn't load memtrack module
2019-09-24 12:01:48.600 23707-23720/? E/memtrack: Couldn't load memtrack module
2019-09-24 12:01:57.722 1396-1396/? E/hw-IPCThreadState: binder thread pool (1 threads) starved for 249 ms
2019-09-24 12:01:57.877 1396-1396/? E/hw-IPCThreadState: binder thread pool (1 threads) starved for 153 ms
2019-09-24 12:01:58.013 1396-1396/? E/hw-IPCThreadState: binder thread pool (1 threads) starved for 136 ms
2019-09-24 12:02:07.079 4851-4851/com.techneo360.pubguide11 E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
2019-09-24 12:02:07.079 4851-4851/com.techneo360.pubguide11 E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
2019-09-24 12:02:13.534 23707-23724/? E/BatteryStatsService: modem info is invalid: ModemActivityInfo{ mTimestamp=0 mSleepTimeMs=0 mIdleTimeMs=0 mTxTimeMs[]=[0, 0, 0, 0, 0] mRxTimeMs=0 mEnergyUsed=0}
2019-09-24 12:02:25.228 4851-4851/com.techneo360.pubguide11 E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
2019-09-24 12:02:27.330 4851-4851/com.techneo360.pubguide11 E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
2019-09-24 12:03:00.019 23707-23720/? E/memtrack: Couldn't load memtrack module
2019-09-24 12:03:19.765 23707-23724/? E/BatteryStatsService: modem info is invalid: ModemActivityInfo{ mTimestamp=0 mSleepTimeMs=0 mIdleTimeMs=0 mTxTimeMs[]=[0, 0, 0, 0, 0] mRxTimeMs=0 mEnergyUsed=0}
2019-09-24 12:03:42.740 23707-23720/? E/memtrack: Couldn't load memtrack module
2019-09-24 12:05:24.921 23707-23720/? E/memtrack: Couldn't load memtrack module
2019-09-24 12:05:43.286 23707-23720/? E/memtrack: Couldn't load memtrack module
2019-09-24 12:08:00.005 23707-23720/? E/memtrack: Couldn't load memtrack module
2019-09-24 12:08:53.884 23707-23724/? E/BatteryStatsService: modem info is invalid: ModemActivityInfo{ mTimestamp=0 mSleepTimeMs=0 mIdleTimeMs=0 mTxTimeMs[]=[0, 0, 0, 0, 0] mRxTimeMs=0 mEnergyUsed=0}
2019-09-24 12:10:00.009 23707-23720/? E/memtrack: Couldn't load memtrack module
2019-09-24 12:11:05.799 23707-23720/? E/memtrack: Couldn't load memtrack module
2019-09-24 12:11:05.810 23707-23720/? E/memtrack: Couldn't load memtrack module
Maddy
  • 4,525
  • 4
  • 33
  • 53
  • You're returning from `onCreateView(..)` on the very first line. The code below that `return` will not execute. Also, the issues with `findViewById` is that you need to find the id on some root view first. So, do `view.findViewById` – denvercoder9 Sep 22 '19 at 11:19

3 Answers3

3

You can not directly call findViewById in fragment. For this you have to implement onViewCreated method, this will return view and u can use this view to call findViewById method.

   @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_send, container, false);
        return view;
    }

     @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        mEditTextTo = view.findViewById(R.id.edit_text_to);
        mEditTextSubject = view.findViewById(R.id.edit_text_subject); 
        mEditTextMessage = view.findViewById(R.id.edit_text_message);
        Button buttonSend = view.findViewById(R.id.button_send);
}
mohammadReza Abiri
  • 1,759
  • 1
  • 9
  • 20
ANK
  • 31
  • 3
1

Init

mEditTextTo = view.findViewById(R.id.edit_text_to);
        mEditTextSubject = view.findViewById(R.id.edit_text_subject); 

inside onViewCreated().

In onCreateView after return statement, it is not allowed as per return

Kishan Maurya
  • 3,356
  • 8
  • 21
1

First of all, you have written findViewById after return statement, so it will never be executed.

Your fragment should be like this

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;


/**
 * A simple {@link Fragment} subclass.
 */
public class Send extends Fragment {

    private EditText mEditTextTo;
    private EditText mEditTextSubject;
    private EditText mEditTextMessage;


    public Send() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_send, container, false);
    }

    @Override
    public View onViewCreated(View view, @Nullable Bundle savedInstanceState) {

        mEditTextTo = view.findViewById(R.id.edit_text_to);
        mEditTextSubject = view.findViewById(R.id.edit_text_subject); 
        mEditTextMessage = view.findViewById(R.id.edit_text_message);

        Button buttonSend = view.findViewById(R.id.button_send);
        buttonSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendMail();
            }
        });
    }

    private void sendMail() {
        String recipientList = mEditTextTo.getText().toString();
        String[] recipients = recipientList.split(",");

        String subject = mEditTextSubject.getText().toString();
        String message = mEditTextMessage.getText().toString();

        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.putExtra(Intent.EXTRA_EMAIL, recipients);
        intent.putExtra(Intent.EXTRA_SUBJECT, subject);
        intent.putExtra(Intent.EXTRA_TEXT, message);

        intent.setType("message/rfc822");
        startActivity(Intent.createChooser(intent, "Choose an email client"));
    }
}
Maddy
  • 4,525
  • 4
  • 33
  • 53