【问题标题】:Need help in setting up code in Fragment (Android Development)在 Fragment 中设置代码时需要帮助(Android 开发)
【发布时间】:2014-03-12 19:33:29
【问题描述】:

显示错误: 我已经在注释中提到了代码中的错误位置..请帮助我无法解决这个问题。 实际上我已经将该代码从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): 开始输入: 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): 开始输入:绑定结果=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): 来自 db 缓存,名称 = sound_effects_enabled ,值 = 0
03-13 01:29:56.428: D/AndroidRuntime(12706): 关闭 VM
03-13 01:29:56.428: W/dalvikvm(12706): threadid=1: 线程以未捕获的异常退出 (group=0x414b79a8)
03-13 01:29:56.433: E/AndroidRuntime(12706): 致命异常: main
03-13 01:29:56.433: E/AndroidRuntime(12706): java.lang.IllegalStateException: 在视图类 android 上的 onClick 处理程序的活动类 com.example.converterplus.MainActivity 中找不到方法 onClick(View)。 id 为“button1”的小部件.Button
03-13 01:29:56.433: E/AndroidRuntime(12706): 在 android.view.View$1.onClick(View.java:3593)
03-13 01:29:56.433: E/AndroidRuntime(12706): 在 android.view.View.performClick(View.java:4211)
03-13 01:29:56.433: E/AndroidRuntime(12706): 在 android.view.View$PerformClick.run(View.java:17446)
03-13 01:29:56.433: E/AndroidRuntime(12706): 在 android.os.Handler.handleCallback(Handler.java:725)
03-13 01:29:56.433: E/AndroidRuntime(12706): 在 android.os.Handler.dispatchMessage(Handler.java:92)
03-13 01:29:56.433: E/AndroidRuntime(12706): 在 android.os.Looper.loop(Looper.java:153)
03-13 01:29:56.433: E/AndroidRuntime(12706): 在 android.app.ActivityThread.main(ActivityThread.java:5297)
03-13 01:29:56.433: E/AndroidRuntime(12706): 在 java.lang.reflect.Method.invokeNative(Native Method)
03-13 01:29:56.433: E/AndroidRuntime(12706): 在 java.lang.reflect.Method.invoke(Method.java:511)
03-13 01:29:56.433: E/AndroidRuntime(12706): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
03-13 01:29:56.433: E/AndroidRuntime(12706): 在 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): 由: java.lang.NoSuchMethodException: onClick [class android.view.View]
03-13 01:29:56.433: E/AndroidRuntime(12706): 在 java.lang.Class.getConstructorOrMethod(Class.java:460)
03-13 01:29:56.433: E/AndroidRuntime(12706): 在 java.lang.Class.getMethod(Class.java:915)
03-13 01:29:56.433: E/AndroidRuntime(12706): 在 android.view.View$1.onClick(View.java:3586)
03-13 01:29:56.433: E/AndroidRuntime(12706): ... 11 更多
03-13 01:29:58.320: I/Process(12706): 发送信号。 PID:12706 SIG:9

    

【问题讨论】:

    标签: java android android-fragments


    【解决方案1】:

    将您的代码更改为:

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

    当您对布局进行膨胀时,您必须通过view.findViewById() 找到该布局中的任何小部件。

    单选按钮错误:

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

    对于 Toast 错误:

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

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

    【讨论】:

    • 非常感谢先生。这些修复了错误。但是当我点击计算按钮时,应用程序仍然强制关闭。请告诉我应该怎么做。这是片段类的 xml 布局:
    • 使用 logcat 错误更新您的答案或提出新问题。
    • XML onClick() 的实现方式是针对活动,而不是片段。活动应该拥有onClick() 方法,而不是片段。最好在活动中以编程方式使用监听器,例如button.setOnClickListener()
    • 谢谢...让我学习一下..(是的,我是菜鸟,制作我生命中的第一个应用程序)我会回复..你很好
    【解决方案2】:
    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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-04
      • 2017-02-08
      • 2014-08-18
      • 1970-01-01
      • 2017-10-16
      • 2014-04-06
      相关资源
      最近更新 更多