在Fragment(Android开发)中设置代码方面需要帮助

时间:2014-03-12 19:33:29

标签: java android android-fragments

显示错误: 我已经在代码中提到错误位置和评论..请帮助我无法解决这个问题。 实际上我已将该代码从Activity类复制到Fragment类。但它显示出以下错误。

我添加了ActionBar个标签片段Viewpager。 为Fragment个人设置Fragment布局和类。

错误1是The method findViewById(int) is undefined for the type TempFragment

错误2是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在点击按钮时崩溃):

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

    

2 个答案:

答案 0 :(得分:1)

将您的代码更改为:

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

当您为布局充气时,您必须通过view.findViewById()找到该布局中的任何小部件。

对于RadioButton错误:

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

对于Toast Error:

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

Toast.makeText()的第一个参数是上下文,在您的情况下,您在片段中调用Toast,因此您必须在此处传递活动上下文。因此,getActivity()将用作此片段附加的活动的上下文。

答案 1 :(得分:0)

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