这个问题在这里已有答案:
当我在EditText上尝试setText()时,我不断收到此错误,我无法看到我出错的地方 . 我想了解这个错误背后的逻辑 . 我确定这是小事,但现在真的让我烦恼 .
01-25 10:20:44.267 14325-14325 / com.example.gregsquibbs.greg_squibbs_todoapp3 E / AndroidRuntime:FATAL EXCEPTION:main进程:com.example.gregsquibbs.greg_squibbs_todoapp3,PID:14325 java.lang.RuntimeException:无法启动activity ComponentInfo {com.example.gregsquibbs.greg_squibbs_todoapp3 / com.example.gregsquibbs.greg_squibbs_todoapp3.EditTodo.EditTodoActivity}:java.lang.NullPointerException:尝试调用虚方法'void android.widget.EditText.setText(java.lang.CharSequence) )'在Android.app.A活动中的一个空对象引用android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817),在android.app.ActivityThread.-wrap11上的android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) . (未知来源: 0)在android.app.A.运行时,Android.O.Roper.loop(Looper.java:164)的android.app.A.运行时,Android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1593)位于android.os.Handler.dispatchMessage(Handler.java:105) java.lang.reflect.Method.i上的android.app.ActivityThread.main(ActivityThread.java:6541) com.android.internal.os.Zygote上的nvoke(Native Method)$ com . android.internal.os.ZygoteInit.main(ZygoteInit.java:767)中的$ MethodAndArgsCaller.run(Zygote.java:240)引起:java . lang.NullPointerException:尝试在com.example.gregsquibbs.greg_squibbs_todoapp3.EditTodo.EditTodoFragment.setMessage(EditTodoFragment.java)的空对象引用上调用虚方法'void android.widget.EditText.setText(java.lang.CharSequence)' . 56)在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java)的android.app.Activity.performCreate(Activity.java:6975)的com.example.gregsquibbs.greg_squibbs_todoapp3.EditTodo.EditTodoActivity.onCreate(EditTodoActivity.java:41) :1213)在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)的android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)在android.app.ActivityThread.-wrap11(未知来源:0)在android android.os.Handler.dispatchMe上的.app.ActivityThread $ H.handleMessage(ActivityThread.java:1593) ssage(Handler.java:105)在android.app.Looper.loop(Looper.java:164)的android.app.ActivityThread.main(ActivityThread.java:6541)at java.lang.reflect.Method.invoke(Native方法)com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:240)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
这是我创建etMessage并使用setText()的地方
public class EditTodoFragment extends Fragment implements EditTodoContract.View {
private EditTodoContract.Presenter presenter;
private EditText etMessage;
public EditTodoFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.edit_todo_fragment, container, false);
System.out.println("Step 2 - Fragment check");
etMessage = (EditText) view.findViewById(R.id.etMessage);
return view;
}
public void setPresenter(EditTodoContract.Presenter todoPresenter) {
this.presenter = todoPresenter;
}
public String getMessage() {
String message = etMessage.getText().toString();
return message;
}
public void setMessage(String message) {
etMessage.setText(message);
}
}
这是XML文件
<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="com.example.gregsquibbs.greg_squibbs_todoapp3.EditTodo.EditTodoActivity">
<EditText
android:id="@+id/etMessage"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:background="@drawable/rect"
android:ems="10"
android:gravity="top"
android:inputType="textShortMessage" />
<Button
android:id="@+id/bSave"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Save"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:onClick="saveButtonClicked"
android:layout_gravity="center_horizontal|bottom"
/>
这是在活动中调用setMessage()的地方
public class EditTodoActivity extends AppCompatActivity {
private String message;
private int position;
private EditTodoFragment editTodoFragment;
private EditTodoPresenter editTodoPresenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.edit_todo_activity);
System.out.println("Step 1");
FragmentManager fm = getSupportFragmentManager();
Fragment fragment = fm.findFragmentById(R.id.contentFrameContainer);
if (fragment == null){
editTodoFragment = new EditTodoFragment();
fm.beginTransaction()
.add(R.id.contentFrameContainer, editTodoFragment)
.commit();
editTodoPresenter = new EditTodoPresenter(editTodoFragment);
}
message = getIntent().getStringExtra(IntentConstants.INTENT_MESSAGE_DATA);
position = getIntent().getIntExtra(IntentConstants.INTENT_ITEM_POSITION, -1);
editTodoFragment.setMessage(message);
}
public void saveButtonClicked(View v) {
if(editTodoFragment.getMessage().equals("")) {
} else {
String changedMessage = (editTodoFragment.getMessage());
Intent intent = new Intent();
intent.putExtra(IntentConstants.INTENT_CHANGED_MESSAGE, changedMessage);
intent.putExtra(IntentConstants.INTENT_ITEM_POSITION, position);
finish();
}
}
}
编辑:片段中的这一行
etMessage = (EditText) view.findViewById(R.id.messageField);
当我将鼠标悬停在铸件(EditText)上时,它说
将'view.findViewById(R.id.messageField)'转换为'EditText'是多余的......(⌘F1)此检查报告不必要的强制转换表达式 .
我不知道这与它有什么关系 .
4 回答
在你的
EditText
被这条线初始化之后,没有任何空间可以调用editTodoFragment.setMessage(message);
etMessage = (EditText) view.findViewById(R.id.etMessage);
在您的情况下,您的代码在初始化之前将文本设置为
EditText
.最佳做法是通过
Bundle
将数据发送到Fragment,并在EditText
初始化后设置Text .检查this回答
Sample Code:
在
EditTodoActivity
在 EditTodoFragment
onCreate
在 EditTodoFragment
onCreateView
可能错误是您在
edit_todo_fragment
之外的其他文件中定义了etMessage
. 这就是为什么你没有得到编译错误,因为对etMessage
的引用存在,但它在你的膨胀视图中不存在,所以你得到了NPE .正如我从日志中所理解的那样,您从
onCreate
方法中调用了setMessage
方法 . 但问题是Activity的onCreate
早于Fragment的onCreateView
被调用,所以你的EditText当时没有被初始化 .首先你必须定义这个 EditText 属于女巫 view 然后,在你给视图充气后你应该这样做 .
如果你之前调用set text,你会看到这个错误
or
此视图(etMessage)不在此布局中(edit_todo_fragment)