在片段中初始化EditText

时间:2015-11-06 11:19:25

标签: android android-fragments android-edittext

我有一个带物品的导航抽屉。点击某个项目时,会打开一个包含edittext,微调器和许多textviews的片段,这些内容会完美显示。 现在,我想在textWatcher添加editText,但应用崩溃了,给了我NullPointerException。我认为这是因为我使用片段,因为完全相同的代码完全正常,如果我将代码放入主Activity。 以下是所有相关代码:

MainActivity.java

    // just before onCreate method
    EditText LengthInput;

    // in the onNavigationItemSelected method, that is
    // responsible for the item clicks, This loads the fragment
    LengthFragment lengthFragment = new LengthFragment();
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.fragment_container, lengthFragment);
    transaction.addToBackStack(null);
    transaction.commit();

    // this method is responsible for the basic functionality
    initializeLengthFragment();
    ...
    // this is later on in the initializeLengthFragment()
    LengthInput = (EditText) findViewById(R.id.LengthInput);
    LengthInput.addTextChangedListener(lengthInputWatcher);

最后一行是发生错误的地方;我在onCreate之前初始化LengthInput并稍后使用它(最后2行)。

length_fragment.xml:

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

<TableLayout
    android:id="@+id/length_fragment"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <TableRow
        android:id="@+id/table1Row1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="5dip" >

        <EditText
            android:id="@+id/LengthInput"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:inputType="numberDecimal"
            android:hint="@string/EditTextHintText"
            android:gravity="center"
            android:background="@android:drawable/edit_text"
            android:textColor="@android:color/primary_text_light" />
        <requestFocus/>
    </TableRow>
...

LengthFragment.java:

package com.example.lukas.navigationdrawer;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class LengthFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.length_fragment, container, false);
    }
}

4 个答案:

答案 0 :(得分:0)

length_fragment.xml EditText位于LengthFragment内,是onViewCreated Fragment而不是Activity的布局。

因此,请使用onCreateView或将 @Override public View onCreateView(....) { // Inflate the layout for this fragment View view=inflater.inflate(R.layout.length_fragment, container, false); EditText LengthInput = (EditText)view. findViewById(R.id.LengthInput); return view; } 方法更改为:

123456 // row id 0,0
234567 // row id 1,0
345678 // row id 2,0
456789 // row id 3,0

答案 1 :(得分:0)

您必须在LengthFragment片段中初始化editText。像这样:

package com.example.lukas.navigationdrawer; 

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class LengthFragment extends Fragment {
EditText lengthInput;
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
    // Inflate the layout for this fragment 
View v = inflater.inflate(R.layout.length_fragment, container, false);
lengthInput = (EditText) v.findViewById(R.id.lengthInput);
lengthInput.addTextChangedListener(lengthInputWatcher);
    return v;
} 

}

答案 2 :(得分:0)

您尚未在代码中的任何位置初始化lengthInputWatcher。在完成所有基本操作(即初始化所有内容)之后,还要放置这些内容。将这些行放在函数的底部。

 initializeLengthFragment();

如果您需要任何帮助,请告诉我并在有帮助的情况下进行标记。

答案 3 :(得分:0)

您的编辑文本需要在片段类中初始化,文本观察者等的所有代码也应该在片段内。

Activity应该只负责携带你的片段,你想要执行的任何其他逻辑或任务应该在你的片段中,并且应该通过覆盖片段的onCreateView方法来完成视图初始化。 这里是片段类的快照。

public class LengthFragment extends Fragment {
EditText LengthInput;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
     View yourFragmentUI = inflater.inflate(R.layout.length_fragment, container, false);
     lengthInput = (EditText) yourFragmentUI .findViewById(R.id.lengthInput);
     lengthInput.addTextChangedListener(lengthInputWatcher);
       return yourFragmentUI;
}
}