Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
358 views
in Technique[技术] by (71.8m points)

android - EditText editable but getText() return empty string

I'm trying to make a simple messaging system in my app. The user type the message in an EditTextat the bottom of the screen. Somehow, the EditTextthat's displayed on the screen isn't actually the one that I get using findViewById().

I tried adding default value to the EditTextin the xml and it works. But if I change the default text, the text on the screen changes but editText.getText()still returns the default value. editText.setText() doesn't change the text displayed on the screen but does change the actual value (editText.getText() returns the new text).

Here's my code:

fragment_message.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context="com.gloriovin.helpio.Views.MessageFragment"
    android:id="@+id/message_fragment"
    android:orientation="vertical">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/sendMessageSection"
        android:id="@+id/listView"
        android:clipToPadding="false"
        android:divider="@color/Transparent"
        android:stackFromBottom="true"
        android:transcriptMode="alwaysScroll"></ListView>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:id="@+id/sendMessageSection"
        android:layout_alignParentBottom="true"
        android:background="@color/LightGrey">

        <com.mikepenz.iconics.view.IconicsImageView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/sendButton"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="3dp"
            app:iiv_size="25dp"
            app:iiv_color="@color/DarkGrey"
            app:iiv_icon="gmd-send" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/messageField"
            android:layout_toLeftOf="@id/sendButton"
            android:singleLine="true"
            android:text="kuda"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="3dp"
            android:gravity="bottom"/>

    </RelativeLayout>

</RelativeLayout>

chatActivity.java

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import com.gloriovin.helpio.R;

public class ChatActivity extends AppCompatActivity {

    private Toolbar toolbar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle("Message");

        MessageFragment messageFragment = new MessageFragment();
    getSupportFragmentManager().beginTransaction().replace(R.id.fragment, messageFragment).    commit();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_message, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

}

messageFragment.java

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.design.widget.FloatingActionButton;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ListView;

import com.gloriovin.helpio.Adapters.friendAdapters;
import com.gloriovin.helpio.Adapters.messageAdapters;
import com.gloriovin.helpio.Globals;
import com.gloriovin.helpio.HelpioApp;
import com.gloriovin.helpio.Models.Message;
import com.gloriovin.helpio.Models.MessageRoom;
import com.gloriovin.helpio.R;
import com.mikepenz.iconics.view.IconicsImageView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.zip.Inflater;

import cz.msebera.android.httpclient.Header;
import io.paperdb.Paper;


public class MessageFragment extends Fragment {
    private Activity activity;
    private int mid;
    public MessageFragment() {
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

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

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mid = getActivity().getIntent().getIntExtra("mid", -1);
        this.activity  = getActivity();
        Log.e("mid", String.valueOf(mid));
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);



        ListView listView = (ListView) getActivity().findViewById(R.id.listView);
        messageAdapters adapter = new messageAdapters(getActivity(), FAKECHAT);
        listView.setDivider(null);
        listView.setAdapter(adapter);


        IconicsImageView sendButton = (IconicsImageView) getActivity().findViewById(R.id.    sendButton);
        sendButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EditText newMessage = (EditText) getActivity().findViewById(R.id.messageField);
                String msg = newMessage.getText().toString();
                Log.e("message is", "asd"+msg);
            }
        });
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }
}

TLDR: in messageFragment.java, the EditText value(.getText()) is different (empty string "") than what actually typed in the EditText. setText()doesn't change the appearance of the EditTextbut does change the result of getText()

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Here:

EditText newMessage = (EditText) getActivity().findViewById(R.id.messageField);

Probably getting NPE Exception because newMessage is null.

EditText with messageField is inside fragment_message layout which is layout of Fragment. so use getView method for initializing newMessage object instead of getActivity() which return the Context of Activity in which Fragment is current available.

override onViewCreated and initialize all views using v.findViewById :

ListView listView;
IconicsImageView sendButton;
EditText newMessage;
public void onViewCreated(View v, Bundle savedInstanceState) {
    super.onViewCreated(v, savedInstanceState);
    listView = (ListView) v.findViewById(R.id.listView);
    sendButton = (IconicsImageView) v.findViewById(R.id.    sendButton);
    newMessage = (EditText) v.findViewById(R.id.messageField);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...