-1

I am trying to make an app to generate random numbers based on range specified by the user.
I had some issue before where when trying to switch to that fragment, it would crash the app, but that was solved because the onClickListener was at the wrong place.
Logcat settings is Verbose and No Filters. When the app crashes nothing comes up, so I am very confused as to why the app is crashing.
This is my first time actually building an app.
Below is the respective Fragment.java file:

    package com.lava.ldc.randomnumbersapp;

import android.content.Context;
import android.net.Uri;
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;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Random;


/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link RandomNumberGeneratorFragment.OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link RandomNumberGeneratorFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class RandomNumberGeneratorFragment extends Fragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private OnFragmentInteractionListener mListener;

    public RandomNumberGeneratorFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment RandomNumberGeneratorFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static RandomNumberGeneratorFragment newInstance(String param1, String param2) {
        RandomNumberGeneratorFragment fragment = new RandomNumberGeneratorFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;

    }

    EditText start;
    EditText end;
    Button btnGenerate;
    TextView randNum;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public void onResume() {
        super.onResume();

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = (RelativeLayout) inflater.inflate(R.layout.fragment_random_number_generator, container, false);
    bindView(view);
    btnGenerate = (Button) view.findViewById(R.id.buttonGen);
    btnGenerate.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v){
            int min = Integer.parseInt(start.getText().toString());
            int max = Integer.parseInt(end.getText().toString());
            Random random = new Random();


            if(min>max){
                Toast toast = new Toast(getActivity().getApplicationContext());
                Toast.makeText(getActivity().getApplicationContext(), "The minimum range value you entered is larger then the maximum range value!", Toast.LENGTH_SHORT).show();
            }
            double randomMultiplier = random.nextDouble();
            long range = (long)max - (long)min + 1;
            int randomNumber = (int)((long)(range * randomMultiplier) + min);
            randNum.setText(randomNumber);
        }
    });
    //Initiated Broadcast receiver
    return view;

}

private void bindView(View view){
        start = (EditText) view.findViewById(R.id.start);
        end = (EditText) view.findViewById(R.id.end);

        randNum = (TextView) view.findViewById(R.id.randomNumber);
}

// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
    if (mListener != null) {
        mListener.onFragmentInteraction(uri);
    }
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    if (context instanceof OnFragmentInteractionListener) {
        mListener = (OnFragmentInteractionListener) context;
    } else {

    }
}

@Override
public void onDetach() {
    super.onDetach();
    mListener = null;
}

/**
 * This interface must be implemented by activities that contain this
 * fragment to allow an interaction in this fragment to be communicated
 * to the activity and potentially other fragments contained in that
 * activity.
 * <p>
 * See the Android Training lesson <a href=
 * "http://developer.android.com/training/basics/fragments/communicating.html"
 * >Communicating with Other Fragments</a> for more information.
 */
public interface OnFragmentInteractionListener {
    // TODO: Update argument type and name
    void onFragmentInteraction(Uri uri);
}}



Below is the Fragment.xml file:

    <RelativeLayout 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.lava.ldc.randomnumbersapp.RandomNumberGeneratorFragment">
    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:alpha="0.5"
        android:background="@drawable/rnbg" />

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:id="@+id/randomNumber"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="0"
        android:textColor="#000000"
        android:textSize="50sp"
        android:textStyle="bold"
        android:layout_marginBottom="85dp"
        android:layout_above="@+id/buttonGen"
        android:layout_centerHorizontal="true" />

    <Button
        android:id="@+id/buttonGen"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="82dp"
        android:elevation="24dp"
        android:text="Generate"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />

    <EditText
        android:id="@+id/start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Enter lower range value"
        android:inputType="number"
        android:layout_above="@+id/end"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="30dp" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="14dp"
        android:text="ENTER RANGE"
        android:textAlignment="center"
        android:textAllCaps="false"
        android:textColor="#000000"
        android:textSize="36sp"
        android:textStyle="bold" />

    <EditText
        android:id="@+id/end"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Enter highest range value"
        android:inputType="number"
        android:layout_marginBottom="76dp"
        android:layout_above="@+id/randomNumber"
        android:layout_alignLeft="@+id/start"
        android:layout_alignStart="@+id/start" />

</RelativeLayout>

I just don't know where I am going wrong. All help appreciated.

Vishal Yadav
  • 3,642
  • 3
  • 25
  • 42

1 Answers1

1

It's possible that randNum.setText(randomNumber); is the cause of the problem.

It is attempting to use randomNumber as a resource id.

See the Android Framework docs for TextView.setText(int) as a reference.

What you need to do is convert randomNumber to a string. For example: randNum.setText(Integer.toString(randomNumber));

calebbrown
  • 934
  • 4
  • 7
  • Oh my God! I cannot believe how I forgot to convert the type at the end. Thank you so much. I have been at this problem for over 5 hours. I accepted the answer, but can't upvote yet to make a difference. I will once I can, again thank you very much. – Lalit Chandwani Oct 11 '17 at 04:28