(需要帮助)在Fragment中使用OnClickListener,在按钮上单击App Force Closes

时间:2014-03-13 18:30:55

标签: java android-fragments onclicklistener

我正在尝试制作温度转换器。 这是一个tab片段类。我为此指定了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 Log的红色标记行

 
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): invalidate [system]: current 476 != cached 0
03-14 00:21:45.223: V/Provider/Settings(2631): from db cache, name = sound_effects_enabled , value = 0
03-14 00:21:45.224: D/AndroidRuntime(2631): Shutting down VM
03-14 00:21:45.224: W/dalvikvm(2631): threadid=1: thread exiting with uncaught exception (group=0x414c59a8)
03-14 00:21:45.228: E/AndroidRuntime(2631): FATAL EXCEPTION: main
03-14 00:21:45.228: E/AndroidRuntime(2631): java.lang.NullPointerException
03-14 00:21:45.228: E/AndroidRuntime(2631):     at com.example.converterplus.TempFragment.onClick(TempFragment.java:46)
03-14 00:21:45.228: E/AndroidRuntime(2631):     at android.view.View.performClick(View.java:4211)
03-14 00:21:45.228: E/AndroidRuntime(2631):     at android.view.View$PerformClick.run(View.java:17446)
03-14 00:21:45.228: E/AndroidRuntime(2631):     at android.os.Handler.handleCallback(Handler.java:725)
03-14 00:21:45.228: E/AndroidRuntime(2631):     at android.os.Handler.dispatchMessage(Handler.java:92)
03-14 00:21:45.228: E/AndroidRuntime(2631):     at android.os.Looper.loop(Looper.java:153)
03-14 00:21:45.228: E/AndroidRuntime(2631):     at android.app.ActivityThread.main(ActivityThread.java:5297)
03-14 00:21:45.228: E/AndroidRuntime(2631):     at java.lang.reflect.Method.invokeNative(Native Method)
03-14 00:21:45.228: E/AndroidRuntime(2631):     at java.lang.reflect.Method.invoke(Method.java:511)
03-14 00:21:45.228: E/AndroidRuntime(2631):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
03-14 00:21:45.228: E/AndroidRuntime(2631):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
03-14 00:21:45.228: E/AndroidRuntime(2631):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

更新

您在视图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;
    }
  } 
}

OLD:

如果我认为这是正确的,那就是第46行崩溃:

if (celsiusButton.isChecked()) 

这意味着你没有在

找到你的celsiusButton
RadioButton celsiusButton = (RadioButton) view.findViewById(R.id.radio0);

您可以在代码中单步执行第46行并仔细检查。

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