当我在导航抽屉中使用带有发送片段的 EditText 时,无法解析片段中的方法“findViewById”? [复制]
Posted
技术标签:
【中文标题】当我在导航抽屉中使用带有发送片段的 EditText 时,无法解析片段中的方法“findViewById”? [复制]【英文标题】:Can not resolve method 'findViewById' in Fragment when i took EditText with Send fragment in Navigation Drawer? [duplicate] 【发布时间】:2020-01-22 17:03:09 【问题描述】: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"));
任何人都无法编写确切的代码 这是我的发送片段的 XML .............................................
<?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_
android:layout_
tools:context=".send">
<!-- TODO: Update blank fragment layout -->
<LinearLayout
android:layout_
android:layout_
android:orientation="vertical">
<TextView
android:layout_
android:layout_
android:text="To:"
android:textAppearance="@style/TextAppearance.AppCompat.Large" />
<EditText
android:id="@+id/edit_text_to"
android:layout_
android:layout_
android:inputType="textEmailAddress" />
<TextView
android:layout_
android:layout_
android:text="Subject:"
android:textAppearance="@style/TextAppearance.AppCompat.Large" />
<EditText
android:id="@+id/edit_text_subject"
android:layout_
android:layout_
android:inputType="textEmailSubject" />
<TextView
android:layout_
android:layout_
android:text="Message:"
android:textAppearance="@style/TextAppearance.AppCompat.Large" />
<EditText
android:id="@+id/edit_text_message"
android:layout_
android:layout_
android:gravity="start|top"
android:lines="10" />
<Button
android:id="@+id/button_send"
android:layout_
android:layout_
android:text="send" />
</LinearLayout>
运行代码时出现Logcat错误
当我运行代码时,它没有显示任何错误,但代码没有正常运行,我现在该怎么办,谁能帮我实现从导航抽屉发送电子邮件。 或者任何人添加发送片段的代码并正常工作并弹出发送邮件??????
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
【问题讨论】:
您从第一行的onCreateView(..)
返回。 return
下面的代码将不会执行。此外,findViewById
的问题是您需要先在某个根视图上找到 id。所以,请view.findViewById
【参考方案1】:
您不能在片段中直接调用 findViewById。为此,您必须实现 onViewCreated 方法,这将返回视图,您可以使用此视图调用 findViewById 方法。
@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);
【讨论】:
【参考方案2】:初始化
mEditTextTo = view.findViewById(R.id.edit_text_to);
mEditTextSubject = view.findViewById(R.id.edit_text_subject);
在 onViewCreated() 中。
在return语句后的onCreateView中,按return是不允许的
【讨论】:
【参考方案3】:首先,你在return
语句之后写了findViewById,所以它永远不会被执行。
你的片段应该是这样的
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"));
【讨论】:
视图显示错误 @JamesLogan 你能添加你的fragment_send.xml
和错误日志吗?
我添加了所有内容
@JamesLogan 避免同时问多个问题,看来您已经更改了实际问题,以前是关于布局,现在是关于 NavDrawer。要问一个好问题,您可以查看this以上是关于当我在导航抽屉中使用带有发送片段的 EditText 时,无法解析片段中的方法“findViewById”? [复制]的主要内容,如果未能解决你的问题,请参考以下文章