我试图在我的应用中制作一个简单的消息系统。用户在屏幕底部的EditText
中键入消息。不知何故,屏幕上显示的EditText
实际上并不是我使用findViewById()
获得的EditText
。
我尝试将默认值添加到xml中的editText.getText()
并且它可以正常工作。但是,如果我更改默认文本,屏幕上的文本会更改,但editText.setText()
仍会返回默认值。 editText.getText()
不会更改屏幕上显示的文字,但会更改实际值(<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>
会返回新文字)。
这是我的代码:
fragment_message.xml
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);
}
}
chatActivity.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();
}
}
messageFragment.java
messageFragment.java
TLDR:
在EditText
中,EditText
值(.getText())与setText()
中实际输入的值不同(空字符串&#34;&#34;)。
EditText
并未更改getText()
的外观,但会更改<?php
final class crud {
private $connexionName, $sql = '';
public function __construct($connexionName) {
$this->connexionName = $connexionName;
}
public final function insert($tableName, $fields=array()){
$this->tableName = $tableName;
$this->fields = $fields;
foreach ($this->fields as $vf) {
$inKeys[] = $vf;
$inKeysDotted[] = ':' . $vf;
$insImKeys = implode(', ', $inKeys);
$insImKeysDotted = implode(', ', $inKeysDotted);
$this->insImKeys = $insImKeys;
$this->insImKeysDotted = $insImKeysDotted;
}
$this->insertedKeys = $inKeys;
$this->insertedKeysDotted = $inKeysDotted;
$sql = "INSERT INTO `$this->tableName` ($this->insImKeys) VALUES ($this->insImKeysDotted);";
//echo $sql.'<br />';
$insertItems = $this->connexionName->prepare($sql);
$this->insertItems = $insertItems;
echo '<pre>';
print_r($insertItems).'<br />';
echo '</pre>';
} // end prepareStm()
public final function checkValType($valToCheck){
$this->valToCheck = $valToCheck;
// http://php.net/manual/en/function.gettype.php
$valType = gettype($this->valToCheck);
$this->valType = $valType;
switch ($this->valType) {
case 'boolean':
$PDOType = PDO::PARAM_BOOL;
break;
case 'integer':
$PDOType = PDO::PARAM_INT;
break;
case 'NULL':
$PDOType = PDO::PARAM_NULL;
break;
default: // string
$PDOType = PDO::PARAM_STR;
break;
}
//echo $PDOType.'<br />';
$this->PDOType = $PDOType;
//return $this->valType;
return $this->PDOType;
} // end checkValType()
public final function bindParams($setValues=array()){
$combine = array_combine($this->insertedKeys, $setValues);
foreach ($combine as $getKey => $getVal) {
$setType = $this->checkValType($getVal);
echo "this->insertItems->bindValue($getKey, $getVal, $setType)<br />";
$this->insertItems->bindValue($getKey, $getVal, $setType);
}
} // end bindParams()
public final function executeQuery(){
return $this->insertItems->execute();
}
}
require_once '../Included_Files/Connect.php';
$con = new crud($connexion);
echo '<br />';
$con->insert('test', array('field1', 'field2', 'field3'));
//$con->checkValType(19);
$con->bindParams(array('<script>alert(\'hello\');</script>', 'pour field2', 'pour field3'));
$con->executeQuery();
的结果
答案 0 :(得分:1)
下面:
EditText newMessage = (EditText) getActivity().findViewById(R.id.messageField);
可能获得NPE Exception
,因为newMessage
是null
。
EditText
的 messageField
位于fragment_message
布局内,这是Fragment的布局。所以使用getView
方法初始化newMessage
对象而不是getActivity()
,它返回片段当前可用的活动上下文。
覆盖onViewCreated
并使用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);
}
答案 1 :(得分:0)
在点击侦听器之外写下此行
EditText newMessage = (EditText) getActivity().findViewById(R.id.messageField);
答案 2 :(得分:0)
您没有正确获取EditText。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mView = inflater.inflate(R.layout.fragment_message, container, false);
newMessage = (EditText) mView.findViewById(R.id.messageField);
String msg = newMessage.getText().toString();
return mView;
}