DipanIns
DipanIns

Reputation: 45

Need help in setting up code in Fragment (Android Development)

Showing error: i have mentioned error position in the code with comment.. please help i cant solve this. Actually i have copied that code from a Activity class to the Fragment class. but its showing follwing errors.

I added ActionBar tabs with fragments,Viewpager. have Fragment layout and class for individual Fragment.

Error 1 is The method findViewById(int) is undefined for the type TempFragment

Error 2 is The method makeText(Context, CharSequence, int) in the type Toast is not applicable for the arguments (TempFragment, String, int)

package com.example.converterplus;

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.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

public class TempFragment extends Fragment { 
private EditText text;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_temp, container, false);

    return rootView;
    text = (EditText) findViewById(R.id.editText1); // ***error 1

}

// this method is called at button click because we assigned the name to the
  // "OnClick property" of the button
  public void onClick(View view) {
    switch (view.getId()) {
    case R.id.button1:
      RadioButton celsiusButton = (RadioButton) findViewById(R.id.radio0); // ***error 1
      RadioButton fahrenheitButton = (RadioButton) findViewById(R.id.radio1); // ***error 1
      if (text.getText().length() == 0) {
        Toast.makeText(this, "Please enter a valid number", // ***error 2
            Toast.LENGTH_LONG).show();
        return;
      }

      float inputValue = Float.parseFloat(text.getText().toString());
      if (celsiusButton.isChecked()) {
        text.setText(String
            .valueOf(ConverterUtil.convertFahrenheitToCelsius(inputValue)));
        celsiusButton.setChecked(false);
        fahrenheitButton.setChecked(true);
      } else {
        text.setText(String
            .valueOf(ConverterUtil.convertCelsiusToFahrenheit(inputValue)));
        fahrenheitButton.setChecked(false);
        celsiusButton.setChecked(true);
      }
      break;
    }
  } 
}

LOGCAT LOG (APP crashes on clicking button):

03-13 01:29:50.708: V/InputMethodManager(12706): onWindowFocus: android.widget.EditText{41951ea0 VFED..CL .F....ID 32,32-452,111 #7f090001 app:id/editText1} softInputMode=288 first=true flags=#1810100
03-13 01:29:50.714: V/InputMethodManager(12706): START INPUT: android.widget.EditText{41951ea0 VFED..CL .F....ID 32,32-452,111 #7f090001 app:id/editText1} ic=com.android.internal.widget.EditableInputConnection@4195af78 tba=android.view.inputmethod.EditorInfo@4195ae68 controlFlags=#107
03-13 01:29:50.716: V/InputMethodManager(12706): Starting input: Bind result=InputBindResult{com.android.internal.view.IInputMethodSession$Stub$Proxy@4195bbf0 com.android.inputmethod.latin/.LatinIME #1325}
03-13 01:29:52.217: I/SurfaceTextureClient(12706): [STC::queueBuffer] (this:0x5dec7898) fps:3.39, dur:1474.09, max:502.67, min:25.80
03-13 01:29:53.221: I/SurfaceTextureClient(12706): [STC::queueBuffer] (this:0x5dec7898) fps:1.99, dur:1003.98, max:502.01, min:501.97
03-13 01:29:53.897: D/VelocityTracker(12706): VelocityTracker: int datax = 10
03-13 01:29:53.897: D/VelocityTracker(12706): VelocityTracker: int m_velocity_magnify_x = 1.000000
03-13 01:29:53.897: D/VelocityTracker(12706): VelocityTracker: int datay = 10
03-13 01:29:53.897: D/VelocityTracker(12706): VelocityTracker: int m_velocity_magnify_y = 1.000000
03-13 01:29:54.226: I/SurfaceTextureClient(12706): [STC::queueBuffer] (this:0x5dec7898) fps:2.98, dur:1005.72, max:502.47, min:75.56
03-13 01:29:55.228: I/SurfaceTextureClient(12706): [STC::queueBuffer] (this:0x5dec7898) fps:2.00, dur:1002.21, max:501.60, min:500.60
03-13 01:29:56.259: I/SurfaceTextureClient(12706): [STC::queueBuffer] (this:0x5dec7898) fps:3.88, dur:1030.73, max:484.34, min:160.27
03-13 01:29:56.422: V/Provider/Settings(12706): invalidate [system]: current 404 != cached 0
03-13 01:29:56.426: V/Provider/Settings(12706): from db cache, name = sound_effects_enabled , value = 0
03-13 01:29:56.428: D/AndroidRuntime(12706): Shutting down VM
03-13 01:29:56.428: W/dalvikvm(12706): threadid=1: thread exiting with uncaught exception (group=0x414b79a8)
03-13 01:29:56.433: E/AndroidRuntime(12706): FATAL EXCEPTION: main
03-13 01:29:56.433: E/AndroidRuntime(12706): java.lang.IllegalStateException: Could not find a method onClick(View) in the activity class com.example.converterplus.MainActivity for onClick handler on view class android.widget.Button with id 'button1'
03-13 01:29:56.433: E/AndroidRuntime(12706):    at android.view.View$1.onClick(View.java:3593)
03-13 01:29:56.433: E/AndroidRuntime(12706):    at android.view.View.performClick(View.java:4211)
03-13 01:29:56.433: E/AndroidRuntime(12706):    at android.view.View$PerformClick.run(View.java:17446)
03-13 01:29:56.433: E/AndroidRuntime(12706):    at android.os.Handler.handleCallback(Handler.java:725)
03-13 01:29:56.433: E/AndroidRuntime(12706):    at android.os.Handler.dispatchMessage(Handler.java:92)
03-13 01:29:56.433: E/AndroidRuntime(12706):    at android.os.Looper.loop(Looper.java:153)
03-13 01:29:56.433: E/AndroidRuntime(12706):    at android.app.ActivityThread.main(ActivityThread.java:5297)
03-13 01:29:56.433: E/AndroidRuntime(12706):    at java.lang.reflect.Method.invokeNative(Native Method)
03-13 01:29:56.433: E/AndroidRuntime(12706):    at java.lang.reflect.Method.invoke(Method.java:511)
03-13 01:29:56.433: E/AndroidRuntime(12706):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
03-13 01:29:56.433: E/AndroidRuntime(12706):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
03-13 01:29:56.433: E/AndroidRuntime(12706):    at dalvik.system.NativeStart.main(Native Method)
03-13 01:29:56.433: E/AndroidRuntime(12706): Caused by: java.lang.NoSuchMethodException: onClick [class android.view.View]
03-13 01:29:56.433: E/AndroidRuntime(12706):    at java.lang.Class.getConstructorOrMethod(Class.java:460)
03-13 01:29:56.433: E/AndroidRuntime(12706):    at java.lang.Class.getMethod(Class.java:915)
03-13 01:29:56.433: E/AndroidRuntime(12706):    at android.view.View$1.onClick(View.java:3586)
03-13 01:29:56.433: E/AndroidRuntime(12706):    ... 11 more
03-13 01:29:58.320: I/Process(12706): Sending signal. PID: 12706 SIG: 9

    

Upvotes: 1

Views: 191

Answers (2)

Manish Dubey
Manish Dubey

Reputation: 4306

Change your code to this:

View rootView = inflater.inflate(R.layout.fragment_temp, container, false);
text = (EditText)rootView.findViewById(R.id.editText1);
return rootView;

As you are inflating a layout so you must find any widget in that layout by view.findViewById().

For RadioButton Error:

RadioButton celsiusButton = (RadioButton) view.findViewById(R.id.radio0);
      RadioButton fahrenheitButton = (RadioButton) view.findViewById(R.id.radio1);

And for Toast Error:

  Toast.makeText(getActivity(), "Please enter a valid number",Toast.LENGTH_LONG).show();  

First parameter of Toast.makeText() is context, and in your case you are calling Toast in fragment so you have to pass activity context here. So, getActivity() will use as context of activity by which this fragment attached.

Upvotes: 1

betteroutthanin
betteroutthanin

Reputation: 7546

RadioButton celsiusButton = (RadioButton) view.findViewById(R.id.radio0); // ***error 1
RadioButton fahrenheitButton = (RadioButton) view.findViewById(R.id.radio1); // ***error 1

Toast.makeText(this.getActivity(), "Please enter a valid number", // ***error 2
        Toast.LENGTH_LONG).show();

Upvotes: 0

Related Questions