簡體   English   中英

Android應用程序片段不斷崩潰

[英]Android App Fragment Keeps Crashing

我正在開發一個簡單的應用程序來轉換溫度。 我試圖弄清楚如何處理用戶輸入的數字,然后處理它並給出轉換。 我在不同的地方搜索並找到了以下代碼的一部分,但是當我真正單擊該按鈕時,我的應用程序崩潰了。

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

您是否建議您完成此教程? 謝謝!

嘗試像這樣更改代碼:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM