当我要将数据从列表传递到任何textview或编辑视图以进行片段化时,我会遇到此问题:尝试在空对象引用上调用虚拟方法'void android.widget.EditText.setText(java.lang.CharSequence)' 我正在使用Butterknife,这是在onCreateView中启动的。怎么了?
片段中的方法:
@Override
public void itemData(int position, List<Task> tasks) {
Task task = tasks.get(position);
taskID = task.getId();
String taskTexT = task.getDate();
titleEdit.setText(task.getText());
Log.d(TAG, "EDIT FRAGMENT " + position + task + taskTexT);
}
碎刀片段
@BindView(R.id.time_edit) TextView timeEdit;
@BindView(R.id.date_edit) TextView dateEdit;
@BindView(R.id.title_edit) EditText titleEdit;
@BindView(R.id.save_edit_button) Button saveEditButton;
和XML文件:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/edit_fragment">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="@color/colorPrimaryDark">
<EditText
android:id="@+id/title_edit"
android:layout_width="330dp"
android:layout_height="60dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="35dp"
android:backgroundTint="#FFF"
android:textColor="#FFF" />
<TextView
android:id="@+id/date_edit"
android:layout_width="130dp"
android:layout_height="40dp"
android:layout_alignTop="@+id/and"
android:layout_toStartOf="@+id/and"
android:clickable="true"
android:focusable="true"
android:gravity="center"
android:hint="Select date"
android:textColor="#FFF"
android:textColorHint="#FFF"
android:textSize="18sp" />
<View
android:id="@+id/line"
android:layout_width="320dp"
android:layout_height="1dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="119dp"
android:background="#FFF" />
<TextView
android:id="@+id/time_edit"
android:layout_width="132dp"
android:layout_height="40dp"
android:layout_above="@+id/line"
android:layout_toEndOf="@+id/and"
android:clickable="true"
android:focusable="true"
android:gravity="center"
android:hint="Select time"
android:textAlignment="center"
android:textColor="#FFF"
android:textColorHint="#FFF"
android:textSize="18sp" />
<TextView
android:id="@+id/and"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_above="@+id/line"
android:layout_centerHorizontal="true"
android:gravity="center"
android:text="and"
android:textColor="#FFF"
android:textSize="18sp" />
<Button
android:id="@+id/save_edit_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="36dp"
android:text="EDIT"/>
</RelativeLayout>
EditFragment:
public class EditFragment extends Fragment implements IRecyclerItemData {
private static final String TAG = "EditFragment";
@BindView(R.id.time_edit) TextView timeEdit;
@BindView(R.id.date_edit) TextView dateEdit;
@BindView(R.id.title_edit) EditText titleEdit;
@BindView(R.id.save_edit_button) Button saveEditButton;
private DatePickerDialog.OnDateSetListener mDateEditListener;
private TimePickerDialog.OnTimeSetListener mTimeEditListener;
List<Task> tasks;
String taskID;
RealmHelper realmHelper;
Realm realm;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.edit_fragment, container, false);
ButterKnife.bind(this, view);
((MainActivity) getActivity()).hideFloatingActionButton();
Realm.init(getActivity());
RealmConfiguration configuration = new RealmConfiguration
.Builder()
.deleteRealmIfMigrationNeeded()
.build();
realm = Realm.getInstance(configuration);
realmHelper = new RealmHelper(realm);
tasks = new ArrayList<>();
tasks = realmHelper.getAllTasks();
mDateEditListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
month = month + 1;
Log.d(TAG, "onDataEditSet: dd/mm/yyyy: " + year + "/" + month + "/" + dayOfMonth);
String date = dayOfMonth + "/" + month + "/" + year;
dateEdit.setText(date);
}
};
mTimeEditListener = new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Log.d(TAG, "onTimeEditSet: hh/mm: " + hourOfDay + "/" + minute);
String time = hourOfDay + ":" + minute;
timeEdit.setText(time);
}
};
return view;
}
@OnClick(R.id.date_edit)
public void onEditDateClick() {
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dialog = new DatePickerDialog(getActivity(), android.R.style.Theme_Holo_Light_Dialog_MinWidth, mDateEditListener,
year, month, day);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.show();
}
@SuppressLint("ResourceType")
@OnClick(R.id.time_edit)
public void onEditTimeClick() {
Calendar calendar = Calendar.getInstance();
int hourOfDay = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
TimePickerDialog dialog = new TimePickerDialog(getActivity(), android.R.style.Theme_Holo_Light_Dialog_MinWidth, mTimeEditListener,
hourOfDay, minute, DateFormat.is24HourFormat(getActivity()));
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.show();
}
@OnClick(R.id.save_edit_button)
public void onEditSaveClick() {
}
@Override
public void itemData(int position, List<Task> tasks) {
Task task = tasks.get(position);
taskID = task.getId();
String taskTexT = task.getDate();
titleEdit.setText(task.getText());
Log.d(TAG, "EDIT FRAGMENT " + position + task + taskTexT);
}
}
这是RV适配器的一部分,我在其中调用itemData:
@Override
public void onBindViewHolder(@NonNull final TaskViewHolder holder, @SuppressLint("RecyclerView") final int position) {
final Task task = tasks.get(position);
holder.textItem.setText(task.getText());
holder.dateItem.setText(task.getDate());
holder.timeItem.setText(task.getTime());
holder.deleteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = holder.getAdapterPosition();
callback.onDeleteClick(position);
notifyItemRemoved(holder.getAdapterPosition());
Log.d(TAG, "DELETE_CLICK " + holder.getAdapterPosition());
}
});
holder.editButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = holder.getAdapterPosition();
callback.onEditClick(position);
callback2.itemData(position, tasks);
}
});
}
MainActivity:
public class MainActivity extends AppCompatActivity implements IRecyclerItemClickListener {
@Nullable
@BindView(R.id.floating_button) FloatingActionButton floatingButton;
RecyclerView recyclerView;
MainAdapter mainAdapter;
EditFragment editFragment = new EditFragment();
Realm realm;
RealmHelper realmHelper;
List<Task> tasks;
String taskID;
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
recyclerView = findViewById(R.id.recycler_view);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
Realm.init(this);
RealmConfiguration configuration = new RealmConfiguration
.Builder()
.deleteRealmIfMigrationNeeded()
.build();
realm = Realm.getInstance(configuration);
realmHelper = new RealmHelper(realm);
tasks = new ArrayList<>();
tasks = realmHelper.getAllTasks();
mainAdapter = new MainAdapter(tasks, this, new IRecyclerItemData() {
@Override
public void itemData(int position, List<Task> tasks) {
editFragment.itemData(position, tasks);
}
});
recyclerView.setAdapter(mainAdapter);
recyclerView.setHasFixedSize(true);
}
@Optional
@OnClick(R.id.floating_button)
public void onClick() {
android.app.Fragment fragment = new TaskFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.main_activity, fragment);
transaction.addToBackStack(null);
transaction.commit();
}
public void showFloatingActionButton() {
floatingButton.show();
}
public void hideFloatingActionButton() {
floatingButton.hide();
}
@Override
public void onBackPressed() {
super.onBackPressed();
showFloatingActionButton();
recyclerView.removeAllViews();
}
@Override
protected void onDestroy() {
realm.close();
super.onDestroy();
}
@Override
public void onDeleteClick(int position) {
Task task = tasks.get(position);
taskID = task.getId();
if (taskID != null) {
realmHelper.deleteTask(taskID);
Log.d(TAG, "ON DELETE CLICK " + taskID);
Toast.makeText(getApplicationContext(), "Task removed", Toast.LENGTH_LONG).show();
}
}
@Override
public void onEditClick(int position) {
Fragment fragment = new EditFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.main_activity, fragment);
transaction.addToBackStack(null);
transaction.commit();
}
}
答案 0 :(得分:1)
确保您要调用 ButterKnife.bind(此视图)进行初始化。
@BindView(R.id.time_edit)
TextView timeEdit;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.your_layout, container, false);
ButterKnife.bind(this, view);
return view;
}
答案 1 :(得分:0)
在您的片段中,在返回ButterKnife.bind(this, view);
之前,在onCreateView();
中调用view
。
答案 2 :(得分:0)
@ user9897182
有时候,当我们的片段已经被破坏时,我们会得到结果。
我建议您将代码包装在:
if(isAdded()&&!getActivity().isFinishing()){
//Todo add your code here
}