每当我切换布局时,片段都会从onCreateView中删除,这使我相信问题出在xml文件本身之内
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation = "vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="40dp"
android:textColor="#FFFFFF"
android:background="@color/lining"
android:text="Añadir actividad"
android:textStyle="bold"/>
<view
android:layout_width="match_parent"
android:layout_height="3dp"
android:layout_marginBottom="10dp"
android:background="@color/lining" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:text="Nombre de actividad"
android:textAlignment="center"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:id="@+id/activity_name"/>
</LinearLayout>
<view
android:layout_width="match_parent"
android:layout_height="3dp"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:background="@color/lining" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:text="Prioridad"
android:textAlignment="center"/>
<Spinner
android:layout_width="match_parent"
android:layout_height="50dp"
android:textSize="20dp"
android:id="@+id/priority"/>
</LinearLayout>
<view
android:layout_width="match_parent"
android:layout_height="3dp"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:background="@color/lining" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Añadir fecha"
android:id="@+id/date_select"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/fecha"
android:textSize="20dp"
android:text="--/--/----"
android:textAlignment="center"/>
</LinearLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:src="@mipmap/ic_launcher" />
</LinearLayout>
AddFragment.java:
public class AddFragment extends Fragment implements View.OnClickListener, DatePickerDialog.OnDateSetListener{
EditText activityName;
Spinner priority;
Button dateSelected;
TextView date;
View v;
Context context;
CallBackToMain backToMain;
Entrada modify;
FloatingActionButton actionButton;
String dateStrung = "";
public static AddFragment newInstance(Entrada e) {
Bundle args = new Bundle();
args.putSerializable("entrada", e);
AddFragment fragment = new AddFragment();
fragment.setArguments(args);
return fragment;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
v = inflater.inflate(R.layout.addfragment, container, false);
return v;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
this.context = context;
backToMain = (CallBackToMain) context;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
instance();
config();
actions();
}
public void instance(){
modify = (Entrada) getArguments().getSerializable("entrada");
date = v.findViewById(R.id.fecha);
dateSelected = v.findViewById(R.id.date_select);
activityName = v.findViewById(R.id.activity_name);
priority = v.findViewById(R.id.priority);
actionButton = v.findViewById(R.id.save);
}
private void config(){
if (modify != null){
activityName.setText(modify.getNombreActividad());
priority.setSelection(modify.getPrioridad());
date.setText(modify.returnDateInFormat());
String [] fullDateSplit = date.getText().toString().split("/");
dateStrung = fullDateSplit[2] + fullDateSplit[1] + fullDateSplit[0];
}
String[] values = {"Alta", "Media", "Baja"};
priority.setAdapter(new ArrayAdapter(context, android.R.layout.simple_list_item_1, values));
}
private void actions(){
actionButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.date_select){
new DatePickerDialog(context, AddFragment.this,
Calendar.getInstance().get(Calendar.getInstance().YEAR),
Calendar.getInstance().get(Calendar.getInstance().MONTH),
Calendar.getInstance().get(Calendar.getInstance().DAY_OF_MONTH)).show();
}
else if (v.getId() == R.id.save){
backToMain.backToMain(new Entrada(0, activityName.getText().toString(), (byte) priority.getSelectedItemPosition(), dateStrung));
}
}
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
//Si no, 0 es enero, 1 es febrero, etc.
month++;
//
if(year < Calendar.YEAR || (year == Calendar.YEAR && month < Calendar.MONTH) || (year == Calendar.YEAR && month == Calendar.MONTH && dayOfMonth < Calendar.DAY_OF_MONTH)){
new AlertDialog.Builder(context).setTitle("Fecha invalida").setMessage("Fecha anterior al día presente").show();
}
else{
dateStrung = year + "" + month + "" + dayOfMonth;
date.setText(dayOfMonth + "/" + month + "/" + year);
}
}
public interface CallBackToMain{
void backToMain(Entrada e);
}
}
它只是停滞不前:
v = inflater.inflate(R.layout.addfragment, container, false);
错误java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
我已经无数次地修改了Java和xml代码,但是我似乎找不到根本原因,而最重要的是它是一个字符串。等号似乎是最大的罪魁祸首,所以我根本不明白会发生什么...
有人可以帮我吗?我有点菜鸟,还不清楚呢^^;
答案 0 :(得分:1)
这是一个简单的错字错误。因为您使用的是以下xml:
main()
这是错误的,因为它不是以大写开头。正确的一个:
<view> .... </>