在OnCreateView()中访问片段组件

时间:2015-09-17 14:56:01

标签: android android-fragments

我按下片段中的按钮时尝试显示Toast,但是当到达return view语句时,应用程序崩溃了。

我只是引用OnCreateView方法中的组件。

MyFragment.java:

package com.example.motassem.navdrawer;

import android.os.Bundle;
import android.support.annotation.Nullable;
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.Toast;

/**
 * Created by Motassem on 9/17/2015.
 */
public class FragmentUpdateCard extends Fragment implements View.OnClickListener {

    private EditText etFirstName, etLastName, etCardNumber, etExpMonth, etExpYear, etCVC;
    private Button btnUpdate;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.frag_update_card, container, false);
        etFirstName = (EditText) v.findViewById(R.id.input_update_fname);
        etLastName = (EditText) v.findViewById(R.id.input_update_lname);
        etExpMonth = (EditText) v.findViewById(R.id.input_update_card_exp_month);
        etExpYear = (EditText) v.findViewById(R.id.input_update_card_exp_year);
        etCardNumber = (EditText) v.findViewById(R.id.input_update_card_number);
        etCVC = (EditText) v.findViewById(R.id.input_update_card_cvc);

        btnUpdate = (Button) v.findViewById(R.id.btn_update);

        btnUpdate.setOnClickListener(this);
        return v;
    }
    @Override
    public void onClick(View v) {
        com.stripe.android.model.Card card = new com.stripe.android.model.Card(
                etCardNumber.getText().toString(),
                Integer.parseInt(etExpMonth.getText().toString()),
                Integer.parseInt(etExpYear.getText().toString()),
                etCVC.getText().toString());

        if (!card.validateCard()) {
            Toast.makeText(getActivity(), "fdsfsdfsfsdf", Toast.LENGTH_LONG).show();
        }
    }
}

layout.xml是一个收集信用卡信息的表格

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingLeft="24dp"
    android:paddingRight="24dp"
    android:paddingTop="26dp">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <android.support.design.widget.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="8dp"
                android:layout_marginTop="8dp">

                <EditText
                    android:id="@+id/input_update_fname"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="First Name"
                    android:inputType="textPersonName" />

            </android.support.design.widget.TextInputLayout>

            <android.support.design.widget.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="8dp"
                android:layout_marginTop="8dp">

                <EditText
                    android:id="@+id/input_update_lname"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="Last Name"
                    android:inputType="textPersonName" />

            </android.support.design.widget.TextInputLayout>

            <android.support.design.widget.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="8dp"
                android:layout_marginTop="8dp">

                <EditText
                    android:id="@+id/input_update_card_number"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="Card Number"
                    android:inputType="number" />

            </android.support.design.widget.TextInputLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <android.support.design.widget.TextInputLayout
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="8dp"
                    android:layout_marginTop="8dp"
                    android:layout_weight="1">

                    <EditText
                        android:id="@+id/input_update_card_exp_month"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:hint="Expiry Month"
                        android:inputType="number" />

                </android.support.design.widget.TextInputLayout>

                <android.support.design.widget.TextInputLayout
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="8dp"
                    android:layout_marginTop="8dp"
                    android:layout_weight="1">

                    <EditText
                        android:id="@+id/input_update_card_exp_year"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:hint="Expiry Year"
                        android:inputType="number" />

                </android.support.design.widget.TextInputLayout>
            </LinearLayout>

            <android.support.design.widget.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="8dp"
                android:layout_marginTop="8dp">

                <EditText
                    android:id="@+id/input_update_card_cvc"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="CVC"
                    android:inputType="number" />

            </android.support.design.widget.TextInputLayout>

            <android.support.v7.widget.AppCompatButton
                android:id="@+id/btn_update"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="24dp"
                android:layout_marginTop="24dp"
                android:padding="12dp"
                android:text="Update Card Information" />
        </LinearLayout>
    </ScrollView>
</LinearLayout>

修改 logcat的

09-17 20:08:48.988    5146-5146/com.example.motassem.navdrawer E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.motassem.navdrawer, PID: 5146
    java.lang.NumberFormatException: Invalid int: ""
            at java.lang.Integer.invalidInt(Integer.java:138)
            at java.lang.Integer.parseInt(Integer.java:358)
            at java.lang.Integer.parseInt(Integer.java:334)
            at com.example.motassem.navdrawer.FragmentUpdateCard.onClick(FragmentUpdateCard.java:45)
            at android.view.View.performClick(View.java:4785)
            at android.view.View$PerformClick.run(View.java:19858)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:155)
            at android.app.ActivityThread.main(ActivityThread.java:5696)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)

3 个答案:

答案 0 :(得分:0)

您应该只访问您的控件并使用onViewCreated方法添加事件侦听器。除非UI构建器抱怨您的布局有问题,否则它应该可以正常工作。所以,也请检查一下。

@Override
    public void onViewCreated(View view, Bundle savedInstanceState) {

答案 1 :(得分:0)

  1. 您的onCreateView需要返回第一次inflate方法调用创建的视图。第二个不需要,实际上创建了另一个视图(哪些孩子)不匹配你的findviewbyid调用(etFirstName,etLastName等...)
  2. click侦听器需要处理错误数据,在您上面发布的堆栈跟踪中,系统正在尝试将空字符串转换为数字,从而导致您看到的NumberFormatException。而是尝试验证输入的字段或处理异常,以便您的应用程序不会崩溃。或尝试使用DatePickerDialog(所以你不必自己弄清楚糟糕的日期,比如2月31日)
  3. 可以使用new View.OnClickListener()(可选)
  4. 内联使用Toast的点击侦听器

    以下是所有(减去DatePickerDialog内容)的示例。

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.frag_update_card, container, false);
        etFirstName = (EditText) v.findViewById(R.id.input_update_fname);
        etLastName = (EditText) v.findViewById(R.id.input_update_lname);
        etExpMonth = (EditText) v.findViewById(R.id.input_update_card_exp_month);
        etExpYear = (EditText) v.findViewById(R.id.input_update_card_exp_year);
        etCardNumber = (EditText) v.findViewById(R.id.input_update_card_number);
        etCVC = (EditText) v.findViewById(R.id.input_update_card_cvc);
    
        btnUpdate = (Button) v.findViewById(R.id.btn_update);
    
        btnUpdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    com.stripe.android.model.Card card =
                        new com.stripe.android.model.Card(etCardNumber.getText().toString(),
                                                          Integer.parseInt(etExpMonth.getText().toString()),
                                                          Integer.parseInt(etExpYear.getText().toString()),
                                                          etCVC.getText().toString());
                    String message = "card valid";
                    if (!card.validateCard()) {
                        message = "card not valid!";
                    }
                } catch (NumberFormatException e) {
                    message = "Enter in a correct date!!!";
                }
                Toast.makeText(v.getContext(), message, Toast.LENGTH_LONG).show();
            }
        });
    
    
        return v;
    }
    

答案 2 :(得分:0)

实际崩溃是由于输入中的整数无效所致。在NumberFormatException中构建卡片对象时,您可以抓住onClick并且无法理解。