在(Sherlock)片段中使用EditText会遇到麻烦

时间:2013-12-30 10:20:07

标签: android android-fragments android-edittext

嘿大家,              我的片段中的编辑文本有些麻烦。我在EditText.getText()。toString()上得到一个NUllPointerException。我知道有一些类似的线程,但我没有运气解决我的问题。我已经尝试将(EditText)getView()。findViewById(...)放在onCreateView()和onActivityCreated()中,但仍然得到NullPointerException。

我附上了(我认为是)相关的代码。如果您需要查看更多代码,请告诉我们。 NullPointerException位于Intent createShareIntent()方法中。我已经评论了我得到异常的那一行。

public class EditNoteFragment extends SherlockFragment {

Long mCurrentPosition;
EditText title;
EditText body;


    // in a fragment class this does not do anything.
    // it is mostly here to register for the menu and context menu. that is all
    // seems silly :/ 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setHasOptionsMenu(true);
    }


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
    Bundle savedInstanceState) {

     if (savedInstanceState != null) {
            mCurrentPosition = savedInstanceState.getLong("RowId");
        }

     View view = inflater.inflate(R.layout.edit_note, container, false);

        title = (EditText) view.findViewById(R.id.title);
        //title.setText("");
         body = (EditText) view.findViewById(R.id.body);
        // body.setText("");


     return view;
}
@Override
public void onPrepareOptionsMenu(Menu menu) {
    super.onPrepareOptionsMenu(menu);
    MenuItem menuShare = menu.findItem(R.id.menu_share);
    ShareActionProvider shareAction = (ShareActionProvider) 
         menuShare.getActionProvider();
    shareAction.setShareIntent(createShareIntent());
}

protected Intent createShareIntent() {

    Intent shareIntent = new Intent(Intent.ACTION_SEND);
    shareIntent.setType("text/plain");
    shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, title.getText().toString()); // From logcat I can see the NullPointerException here
    shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, body.getText().toString());
    return shareIntent;
}

按要求提供相关的xml

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

<EditText
    android:id="@+id/title"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:hint="@string/titlehint"
    android:inputType="textCapSentences"
    android:singleLine="true"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textStyle="bold" />

<EditText
    android:id="@+id/body"
    android:layout_width="match_parent"
    android:layout_height="0dip"
    android:layout_margin="5dp"
    android:layout_weight="1"
    android:fadeScrollbars="true"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:gravity="top"
    android:hint="@string/noteshint"
    android:inputType="textMultiLine"
    android:scrollbars="vertical" />

任何帮助将不胜感激。 欢呼声。

2 个答案:

答案 0 :(得分:1)

只需这样做。

if(title!=null){
    final String text = title.getText();
    if (!TextUtils.isEmpty(text)) {
        // creates the intent and fires it...
    } else {
        // shows an error message that the text is required.
    }

}

必然会发挥作用

答案 1 :(得分:0)

仅当编辑文本中包含任何文本字段时,EditText.getText()的方法才会检索类型为Editable的对象(可编辑是用户在带有图形属性的框中填充的文本的乘积)

您应该执行以下操作:

final String text = title.getText() != null ? title.getText().toString() : null;
if (!TextUtils.isEmpty(text)) {
    // creates the intent and fires it...

} else {
    // shows an error message that the text is required.
}

希望它有所帮助.. :)