【问题标题】:(Need Help) Using OnClickListener in Fragment, On Button Click App Force Closes(需要帮助)在 Fragment 中使用 OnClickListener,On Button Click App Force Closes
【发布时间】:2014-03-14 03:08:50
【问题描述】:

我正在尝试制作温度转换器。 这是一个选项卡片段类。我为此指定了xml。其中包括一个带有 android:onClick="onClick"

的按钮

这里是 TempFragment.java 类的源代码。 当我单击按钮时,应用程序强制关闭。 请帮忙调试

package com.example.converter;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

public class TempFragment extends Fragment 
  implements OnClickListener 
  {
  private EditText text;
  Button b;

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

    View view = inflater.inflate(R.layout.fragment_temp, container, false);
    text = (EditText) view.findViewById(R.id.editText1);
    b = (Button) view.findViewById(R.id.button1);
    b.setOnClickListener(this);         
    return view;

}

// 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) view.findViewById(R.id.radio0);
      RadioButton fahrenheitButton = (RadioButton) view.findViewById(R.id.radio1);
      if (text.getText().length() == 0) {
          Toast.makeText(getActivity(), "Please enter a valid number", 
                  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 日志中的红色标记行

03-14 00:21:44.711: I/SurfaceTextureClient(2631): [STC::queueBuffer] (this:0x5df0bd48) fps:1.99, dur:1003.79, max:501.96, min:501.83 03-14 00:21:45.218: V/Provider/Settings(2631): 无效 [系统]: 当前 476 != 缓存 0 03-14 00:21:45.223: V/Provider/Settings(2631): 来自 db 缓存,名称 = sound_effects_enabled ,值 = 0 03-14 00:21:45.224: D/AndroidRuntime(2631): 关闭虚拟机 03-14 00:21:45.224: W/dalvikvm(2631): threadid=1: 线程以未捕获的异常退出 (group=0x414c59a8) 03-14 00:21:45.228:E/AndroidRuntime(2631):致命异常:主要 03-14 00:21:45.228: E/AndroidRuntime(2631): java.lang.NullPointerException 03-14 00:21:45.228: E/AndroidRuntime(2631): 在 com.example.converterplus.TempFragment.onClick(TempFragment.java:46) 03-14 00:21:45.228: E/AndroidRuntime(2631): 在 android.view.View.performClick(View.java:4211) 03-14 00:21:45.228: E/AndroidRuntime(2631): 在 android.view.View$PerformClick.run(View.java:17446) 03-14 00:21:45.228: E/AndroidRuntime(2631): 在 android.os.Handler.handleCallback(Handler.java:725) 03-14 00:21:45.228: E/AndroidRuntime(2631): 在 android.os.Handler.dispatchMessage(Handler.java:92) 03-14 00:21:45.228: E/AndroidRuntime(2631): 在 android.os.Looper.loop(Looper.java:153) 03-14 00:21:45.228: E/AndroidRuntime(2631): 在 android.app.ActivityThread.main(ActivityThread.java:5297) 03-14 00:21:45.228: E/AndroidRuntime(2631): 在 java.lang.reflect.Method.invokeNative(Native Method) 03-14 00:21:45.228: E/AndroidRuntime(2631): 在 java.lang.reflect.Method.invoke(Method.java:511) 03-14 00:21:45.228: E/AndroidRuntime(2631): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833) 03-14 00:21:45.228: E/AndroidRuntime(2631): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) 03-14 00:21:45.228: E/AndroidRuntime(2631): 在 dalvik.system.NativeStart.main(Native Method)

【问题讨论】:

  • 向我们提供堆栈跟踪。这样我们就可以立即知道出了什么问题,而不必阅读您的所有代码。只需编辑您的问题并粘贴您的堆栈跟踪。
  • 先生,我不知道什么是“堆栈跟踪”。我是新人。请
  • 只需使用您的编辑器搜索“stacktrace”即可。 “eclipse stacktrace”或“logcat eclipse”——然后单击图像选项卡,您将获得数以千计的答案。例子。 google.se/…
  • 我从 logcat 日志中更新了那些红色标记的行。请检查我是否正确

标签: java android-fragments onclicklistener


【解决方案1】:

更新

您在视图onClick(View view){..}. 中获得了一个按钮,并且您的“radio0”未在该视图中定义。您希望 view 成为布局视图。

见代码:

package com.example.converter;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

public class TempFragment extends Fragment 
  implements OnClickListener 
  {
  private EditText text;
  Button b;
  View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    view = inflater.inflate(R.layout.fragment_temp, container, false);
    text = (EditText) view.findViewById(R.id.editText1);
    b = (Button) view.findViewById(R.id.button1);
    b.setOnClickListener(this);         
    return view;

}

// this method is called at button click because we assigned the name to the
  // "OnClick property" of the button
  public void onClick(View v) {
    switch (v.getId()) {
    case R.id.button1:
      RadioButton celsiusButton = (RadioButton) view.findViewById(R.id.radio0);
      RadioButton fahrenheitButton = (RadioButton) view.findViewById(R.id.radio1);
      if (text.getText().length() == 0) {
          Toast.makeText(getActivity(), "Please enter a valid number", 
                  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;
    }
  } 
}

旧:

如果我猜对了,那就是第 46 行崩溃了:

if (celsiusButton.isChecked()) 

这意味着您没有找到您的 celsiusButton

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

您可以降级到代码中的第 46 行并仔细检查。

如果它是正确的行,那么你需要仔细检查你的按钮是否在你的 xml 中被称为“radio0”。

【讨论】:

  • 我检查他们没问题。实际上我已经从一个运行良好的活动类中复制了代码。现在在片段中布局正常但不起作用。
  • 非常感谢..您的代码正在运行。感谢您帮助这个菜鸟..我可以通过任何信使(gtak,FB,Whatsapp)与您联系吗?不会时不时打扰你的承诺。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多