Question

I am trying to make an temperature converter. this is a tab fragment class. i have xml specified for this. which includes a button with android:onClick="onClick"

Here is the TempFragment.java class source. when i click on button the app force closes. Please help Debug

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;
    }
  } 
}

UPDATE: Red marked lines from 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)

Was it helpful?

Solution

UPDATE

You are getting a button in your view onClick(View view){..}. and your "radio0" is not defined in that view. You want view to be the layout view.

See code:

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:

If I get this correct, it's line no 46 that's crashing:

if (celsiusButton.isChecked()) 

which means you are not finding your celsiusButton at

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

You can step down to line no 46 in your code and double check this.

If it's the correct line then you need to double check if your button is called "radio0" in your xml.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top