2017-10-11 71 views
-1

我想讓應用程序根據用戶指定的範圍生成隨機數。
在嘗試切換到該片段之前,我遇到了一些問題,它會使應用程序崩潰,但由於onClickListener位於錯誤位置,因此解決了這個問題。
Logcat設置是詳細且無過濾器。當應用程序崩潰時,什麼都沒有出現,所以我很困惑爲什麼應用程序崩潰。
這是我第一次真正構建一個應用程序。
下面是各自Fragment.java文件:點擊按鈕後應用程序崩潰(在logcat中沒有錯誤)

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> 

我只是不知道我要去的地方錯了。 所有幫助表示讚賞。

+0

嗨Lalit請把你的崩潰日誌在這裏,所以我們可以幫助你解決問題。 –

+0

崩潰日誌中沒有任何內容,正如我提到的:( –

回答

1

有可能randNum.setText(randomNumber);是問題的原因。

它試圖使用randomNumber作爲資源ID。

請參閱Android框架文檔TextView.setText(int)作爲參考。

你需要做的是將randomNumber轉換爲一個字符串。例如: randNum.setText(Integer.toString(randomNumber));

+0

哦,我的天啊!我不敢相信我最後忘記了怎麼轉換類型,非常感謝,我一直在這個問題上超過5個小時。答案是肯定的,但不能贊成,但我會盡我所能,再次非常感謝你。 –

相關問題