0

Fragment makes it out of onCreateView whenever I switch out the layout, leading me to believe the problem is within the xml file itself

<?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>

the 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);
    }
}

It simply comes to a dead halt in:

v = inflater.inflate(R.layout.addfragment, container, false);

with the error java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference

I've revised the java and the xml code countless times, but I can't seem to find the root cause of this, and to top it off it's a string.equals that seems to be the biggest culprit here, so I simply don't understand what could be happening...

Can anyone lend me a hand? I'm a bit of a rookie, it that hasn't been made clear yet ^^;

ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
omeggga
  • 11
  • 3
  • The logcat message should be followed by the file that's causing the exception complete with line number. It's usually the first blue link you find after the exception stack trace. Post the complete stack trace if you still can't find it. – user1504495 Jun 22 '18 at 00:14
  • @user1504495 at com.example.omega.taskcon.fragments.AddFragment.onCreateView(AddFragment.java:57) – omeggga Jun 22 '18 at 00:45
  • @user1504495 I found out that if I remove the views it works fine... wtf? – omeggga Jun 22 '18 at 01:03

1 Answers1

1

This is a simple typo error. Because you're using the following xml:

 <view> .... </>

which is incorrect because it's not starting with uppercase. The correct one:

 <View> .... </>
ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
  • I'll try that once I'm home and check your answer if it works, but it's weird, the preview seems to acknowledge the lowercase just fine... – omeggga Jun 22 '18 at 06:48
  • You're right, the preview didn't complain at all. But it will gives you runtime error. I've wasted some times because of the same error. – ישו אוהב אותך Jun 22 '18 at 06:57