共享偏好,节省麻烦

时间:2017-10-23 06:48:21

标签: android sharedpreferences

这个聊天窗口项目出现问题时,调用ChatWindow活动时它会崩溃....

在单击服务器场的调试器中,我在窗口中看到此消息:

@FastNative
static native Class<?> classForName(String className, boolean shouldInitialize,
        ClassLoader classLoader) throws ClassNotFoundException;

我还在“变量”窗口中看到以下消息:

Exception = {ClassNotFoundException@3804}

以下是ChatWindow代码:

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;


    }
}



}

0 个答案:

没有答案