Android 共享参考,省事
此聊天窗口项目遇到问题,当调用聊天窗口活动时,它会崩溃 在调试器中单击服务器场时,我在窗口中看到以下消息:Android 共享参考,省事,android,sharedpreferences,Android,Sharedpreferences,此聊天窗口项目遇到问题,当调用聊天窗口活动时,它会崩溃 在调试器中单击服务器场时,我在窗口中看到以下消息: @FastNative static native Class<?> classForName(String className, boolean shouldInitialize, ClassLoader classLoader) throws ClassNotFoundException; Exception = {ClassNotFoundExcepti
@FastNative
static native Class<?> classForName(String className, boolean shouldInitialize,
ClassLoader classLoader) throws ClassNotFoundException;
Exception = {ClassNotFoundException@3804}
这是聊天窗口代码:
public class ChatWindow extends AppCompatActivity {
ListView myDisplay;
EditText myChat;
Button mySend;
ArrayList<String> myList;
ChatAdapter messageAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat_window);
//declarations
myDisplay = (ListView) findViewById(R.id.display);
myChat = (EditText) findViewById(R.id.chat);
mySend = (Button) findViewById(R.id.sndBtn);
myList = new ArrayList<>();
//in this case, “this” is the ChatWindow, which is-A Context object
messageAdapter =new ChatAdapter( this );
myDisplay.setAdapter (messageAdapter);
//send button actions
mySend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String message;
message = myChat.toString();
myList.add(message);
messageAdapter.notifyDataSetChanged(); //this restarts the process of getCount()/getView()
myChat.setText("");
}
});
}
private class ChatAdapter extends ArrayAdapter<String> {
public ChatAdapter(Context ctx) {
super(ctx, 0);
}
public int getCount() {
return myList.size();
}
public String getItem(int position) {
return myList.get(position);
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = ChatWindow.this.getLayoutInflater();
View result = null ;
if(position%2 == 0)
result = inflater.inflate(R.layout.chat_row_incoming, null);
else
result = inflater.inflate(R.layout.chat_row_outgoing, null);
TextView message = (TextView)result.findViewById(R.id.message_text);
message.setText( getItem(position) ); // get the string at position
return result;
}
}
}
公共类聊天窗口扩展了AppCompative活动{
列表视图显示;
编辑文本myChat;
按钮mySend;
ArrayList myList;
聊天适配器消息适配器;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u chat\u窗口);
//声明
myDisplay=(ListView)findViewById(R.id.display);
myChat=(EditText)findViewById(R.id.chat);
mySend=(按钮)findviewbyd(R.id.sndBtn);
myList=新的ArrayList();
//在本例中,“this”是聊天窗口,它是一个上下文对象
messageAdapter=新的聊天适配器(此);
myDisplay.setAdapter(messageAdapter);
//发送按钮动作
mySend.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
字符串消息;
message=myChat.toString();
myList.add(消息);
messageAdapter.notifyDataSetChanged();//这将重新启动getCount()/getView()的进程
myChat.setText(“”);
}
});
}
私有类ChatAdapter扩展了ArrayAdapter{
公共聊天适配器(上下文ctx){
super(ctx,0);
}
public int getCount(){
返回myList.size();
}
公共字符串getItem(int位置){
返回myList.get(位置);
}
公共视图getView(int位置、视图转换视图、视图组父视图){
LayoutInflater充气器=聊天窗口。this.getLayoutInflater();
查看结果=空;
如果(位置%2==0)
结果=充气机充气(R.layout.chat\u row\u传入,空);
其他的
结果=充气机。充气(R.layout.chat\u row\u outing,null);
TextView message=(TextView)result.findViewById(R.id.message\u text);
message.setText(getItem(position));//获取位置处的字符串
返回结果;
}
}
}
此问题与SharedReference有何关系?此外,如果您可以发布日志,这将有助于更好地解决问题。没有与SharedReference相关的代码,为什么要这样做“message=myChat.toString();”这个问题与SharedReference有什么关系?此外,如果您可以发布日志,这将有助于更好地解决问题。没有与SharedReference相关的代码,为什么要这样做“message=myChat.toString();”