简体   繁体   中英

Android App Fragment Keeps Crashing

I am working on a simple app to convert the temperature. I am trying to figure out how to process a number that a user inputs and then process it and give out the conversion. I searched different places and found part of the following code but my app crashes when I actually click on the button.

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.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.view.View.OnClickListener;    
import org.w3c.dom.Text;

public class CelsiusFragment extends Fragment implements OnClickListener {        
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.celsiusfragment, container, false);

            Button celsiusbutton = (Button) view.findViewById(R.id.button_celsius);
            celsiusbutton.setOnClickListener(this);            
        return view;
    }

    Button mButton;
    EditText mEdit;
    TextView mText;
   int output;

    @Override
    public void onClick(View view) {

        mButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {        
                int number = 0;
                int number1 = number;
                output = number1 + 2;
                TextView result = (TextView) view.findViewById(R.id.result);
                result.setText(String.valueOf(output));        
            }        
        });
    }
}

Is there a tutorial that you would recommend to accomplish this? Thanks!

Try to change your code like this:

fragment.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <EditText
        android:id="@+id/value"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:ems="10"
        android:layout_gravity="center_horizontal" />

    <TextView
        android:id="@+id/result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <Button
        android:id="@+id/calculate_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Calculate"
        android:layout_gravity="center_horizontal" />
</LinearLayout>

CelsiusFragment.java

public class CelsiusFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment, container, false);

        Button calculateButton = (Button) view.findViewById(R.id.calculate_button);
        calculateButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                TextView result = (TextView) view.findViewById(R.id.result);
                EditText value = (EditText) view.findViewById(R.id.value);
                int output = Integer.parseInt(value.getText().toString()) + 2;
                result.setText(String.valueOf(output));
            }
        });
        return view;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM