单击片段中的按钮后应用程序崩溃(logcat中没有错误!)

时间:2017-10-11 04:08:47

标签: java android android-fragments

我正在尝试创建一个应用程序,根据用户指定的范围生成随机数 在尝试切换到该片段之前,我遇到了一些问题,它会使应用程序崩溃,但这已经解决,因为onClickListener位于错误的位置。
Logcat设置是详细和无过滤器。当应用程序崩溃时,没有任何内容出现,所以我对应用程序崩溃的原因感到非常困惑 这是我第一次真正构建应用程序 下面是相应的Fragment.java文件:

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



下面是Fragment.xml文件:

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

我只是不知道自己哪里出错了。 所有帮助表示赞赏。

1 个答案:

答案 0 :(得分:1)

randNum.setText(randomNumber);可能导致问题。

它正在尝试使用randomNumber作为资源ID。

请参阅TextView.setText(int)的Android Framework文档作为参考。

您需要做的是将randomNumber转换为字符串。例如: randNum.setText(Integer.toString(randomNumber));