Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:自定义适配器无法在活动恢复时重新加载列表视图_Android_Android Listview_Android Activity_Android Adapter_Android Handler - Fatal编程技术网

Android:自定义适配器无法在活动恢复时重新加载列表视图

Android:自定义适配器无法在活动恢复时重新加载列表视图,android,android-listview,android-activity,android-adapter,android-handler,Android,Android Listview,Android Activity,Android Adapter,Android Handler,我正在尝试使用列表视图上的自定义适配器显示收到的消息,直到现在一切正常,但当我按“上一步”并返回并再次启动此活动时,只有message send起作用,但列表视图上无法显示收到的消息 到目前为止,我已经采取了以下步骤: 使用调试器查明消息是否为空,消息是否为空 已接收且不为空 在receives()函数中使用调试器,它首先打开 启动activity looper.loop()函数将控制权返回给 主线程,但下次活动恢复时,此循环不会 似乎结束了(可能是这是错误的原因) p、 s:在第一次启动此活

我正在尝试使用列表视图上的自定义适配器显示收到的消息,直到现在一切正常,但当我按“上一步”并返回并再次启动此活动时,只有message send起作用,但列表视图上无法显示收到的消息

到目前为止,我已经采取了以下步骤:

  • 使用调试器查明消息是否为空,消息是否为空 已接收且不为空

  • 在receives()函数中使用调试器,它首先打开 启动activity looper.loop()函数将控制权返回给 主线程,但下次活动恢复时,此循环不会 似乎结束了(可能是这是错误的原因)

    p、 s:在第一次启动此活动时,我可以显示收到的消息,但当我返回并继续活动时,receives()失败,我在这里不知道,欢迎向正确方向提供任何小提示,谢谢时间

    公共类hotListener扩展了ListActivity{

    private XMPPConnection connection;
    private IBinder binder;
    private Handler mHandler = new Handler();
    private ArrayList<String> messages = new ArrayList<String>();
    ArrayList<ChatMessage> messagex= new ArrayList<ChatMessage>();;
    ChattingAdapter adaptex;
    Intent mIntent ;
    private ListView listview;
    EditText sender_message ;
    String msg;
    
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listener);
        //messagex.add(new ChatMessage("Hello", false));
        adaptex  = new ChattingAdapter(getApplicationContext(),messagex);
        setListAdapter(adaptex);
    
        Button send_button = (Button) findViewById(R.id.chat_send_message);
        sender_message = (EditText) findViewById(R.id.chat_input);
        send_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                msg = sender_message.getText().toString();
                sender_message.setText("");
                if(!(msg.length()==0)){
                    messagex.add(new ChatMessage(msg, true));
                    //addNewMessage(new ChatMessage(msg, true));
                    adaptex.notifyDataSetChanged();
                    getListView().setSelection(messagex.size()-1);
                }
    
            }
        });
        if(!isMyServiceRunning()){
            System.out.println("seems like service not running");
            startService(new Intent(this,xService.class));
            System.out.print(" now started ");
        }       
    }
    
    @Override
    protected void onStart(){
        super.onStart();
        Boolean kuch = bindService(new Intent(this,xService.class), mConnection,Context.BIND_AUTO_CREATE);
        //System.out.println(kuch);
        //System.out.println("bind done");        
    }
    
    private void receives(XMPPConnection connection2) {
            //ChatManager chatmanager = connection.getChatManager();
            connection2.getChatManager().addChatListener(new ChatManagerListener() {
    
                @Override
                public void chatCreated(Chat arg0, boolean arg1) {
                    arg0.addMessageListener(new MessageListener() {
    
                        @Override
                        public void processMessage(Chat chat, Message message) {
    
                            final String from = message.getFrom();
                            final String body = message.getBody();
                            mHandler.post(new Runnable() {
                                ChatMessage kudi = new ChatMessage(body, false);
                                @Override
                                public void run() {
                                    //setListAdapter(1);
                                    messagex.add(kudi);
                                    adaptex.notifyDataSetChanged();
                                    getListView().setSelection(messagex.size()-1);
                                    Toast.makeText(hotListener.this,body,Toast.LENGTH_SHORT).show();                                }
                            });
                            //System.out.println(String.format("Received message "+body +" from "+ from));
                        }
                    });
                }
            });
    }
    
    private boolean isMyServiceRunning() {
        ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        for(RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)){
            if(xService.class.getName().equals(service.service.getClassName())){
                return true;
            }
        }
        //System.out.print("false");
        return false;
    }
    
    @Override
      protected void onResume() {
        bindService(new Intent(this, xService.class), mConnection, Context.BIND_AUTO_CREATE);
        setListAdapter(adaptex);
        adaptex.notifyDataSetChanged();
        super.onResume();
      }
     @Override
      protected void onPause() {
        unbindService(mConnection);
        super.onPause();
      }
    
     private ServiceConnection mConnection = new ServiceConnection() {
    
            @Override
            public void onServiceDisconnected(ComponentName name) {
                connection = null;
                service = null;
            }
    
            @Override
            public void onServiceConnected(ComponentName name, IBinder binder) {
                //System.out.println("binding in hot listener");
                service = ((xService.MyBinder)binder).getService();
                connection = service.getConnection();
                receives(connection);
                Log.wtf("Service","connected");
            }
        };
    
        void addNewMessage(ChatMessage m)
        {
            System.out.println("1");
            messagex.add(m);
            System.out.println("2");
            adaptex.notifyDataSetChanged();
            System.out.println("3");
            getListView().setSelection(messagex.size()-1);
        }
    
    私有XMPPConnection连接;
    私人IBinder活页夹;
    私有处理程序mHandler=新处理程序();
    private ArrayList messages=new ArrayList();
    ArrayList messagex=新的ArrayList();;
    ChattingAdaptex;
    意向性薄荷糖;
    私有列表视图列表视图;
    编辑文本发送者信息;
    串味精;
    @凌驾
    创建时受保护的void(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.listener);
    //add(newchatmessage(“Hello”,false));
    adaptex=新聊天适配器(getApplicationContext(),messagex);
    setListAdapter(adaptex);
    按钮发送按钮=(按钮)findViewById(R.id.chat\u发送消息);
    发件人消息=(编辑文本)findViewById(R.id.chat\u输入);
    send_button.setOnClickListener(新视图.OnClickListener(){
    @凌驾
    公共void onClick(视图v){
    msg=sender_message.getText().toString();
    发送者_message.setText(“”);
    如果(!(消息长度()=0)){
    添加(新的聊天信息(msg,true));
    //addNewMessage(newchatmessage(msg,true));
    adaptex.notifyDataSetChanged();
    getListView().setSelection(messagex.size()-1);
    }
    }
    });
    如果(!isMyServiceRunning()){
    System.out.println(“似乎服务没有运行”);
    startService(新意图(这个,xService.class));
    系统输出打印(“现在开始”);
    }       
    }
    @凌驾
    受保护的void onStart(){
    super.onStart();
    布尔值kuch=bindService(新意图(this,xService.class),mConnection,Context.BIND\u AUTO\u CREATE);
    //系统输出println(kuch);
    //System.out.println(“绑定完成”);
    }
    私有void接收(XMPPConnection connection2){
    //ChatManager ChatManager=connection.getChatManager();
    connection2.getChatManager().addChatListener(新ChatManagerListener()){
    @凌驾
    已创建公共无效聊天室(聊天室arg0,布尔值arg1){
    arg0.addMessageListener(新MessageListener(){
    @凌驾
    public void processMessage(聊天室、消息消息){
    最后一个字符串from=message.getFrom();
    最后一个字符串body=message.getBody();
    mHandler.post(新Runnable(){
    ChatMessage kudi=新ChatMessage(body,false);
    @凌驾
    公开募捐{
    //设置列表适配器(1);
    messagex.add(kudi);
    adaptex.notifyDataSetChanged();
    getListView().setSelection(messagex.size()-1);
    Toast.makeText(hotListener.this,body,Toast.LENGTH_SHORT).show();}
    });
    //System.out.println(String.format(“接收到的消息”+body+“来自”+from));
    }
    });
    }
    });
    }
    私有布尔值isMyServiceRunning(){
    ActivityManager=(ActivityManager)getSystemService(Context.ACTIVITY_服务);
    for(RunningServiceInfo服务:manager.getRunningServices(Integer.MAX_值)){
    if(xService.class.getName().equals(service.service.getClassName())){
    返回true;
    }
    }
    //系统输出打印(“假”);
    返回false;
    }
    @凌驾
    受保护的void onResume(){
    bindService(新意图(this,xService.class),mConnection,Context.BIND\u AUTO\u CREATE);
    setListAdapter(adaptex);
    adaptex.notifyDataSetChanged();
    super.onResume();
    }
    @凌驾
    受保护的void onPause(){
    解除绑定服务(mConnection);
    super.onPause();
    }
    专用ServiceConnection mConnection=新ServiceConnection(){
    @凌驾
    ServiceDisconnected上的公共无效(组件名称){
    连接=空;
    服务=空;
    }
    @凌驾
    服务连接上的公共void(组件名称,IBinder绑定器){
    //System.out.println(“热侦听器中的绑定”);
    服务=((xService.MyBinder)binder.getService();
    connection=service.getConnection();
    接收(连接);
    Log.wtf(“服务”、“连接”);
    }
    };
    void addNewMessage(ChatMessage)
    {
    系统输出打印项次(“1”);
    messagex.add(m);
    系统输出打印项次(“2”);
    adaptex.notifyDataSetChanged();
    系统输出打印项次(“3”);
    getListView().setSelection(messagex.size()-1);
    }
    
    }

我的适配器:

import java.util.ArrayList;

import android.content.Context;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

public class ChattingAdapter extends BaseAdapter{
    private Context mContext;
    private ArrayList<ChatMessage> mMessages;


    public ChattingAdapter(Context context, ArrayList<ChatMessage> messages) {
        super();
        this.mContext = context;
        this.mMessages = messages;
    }
    @Override
    public int getCount() {
        return mMessages.size();
    }
    @Override
    public Object getItem(int position) {       
        return mMessages.get(position);
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ChatMessage message = (ChatMessage) this.getItem(position);

        ViewHolder holder; 
        if(convertView == null)
        {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.listitem, parent, false);
            holder.message = (TextView) convertView.findViewById(R.id.text1);
            convertView.setTag(holder);
        }
        else
            holder = (ViewHolder) convertView.getTag();

        holder.message.setText(message.getMessage());
        LayoutParams lp = (LayoutParams) holder.message.getLayoutParams();      

        //Check whether message is mine to show green background and align to right

        if(message.isMine())

        {           holder.message.setBackgroundResource(R.drawable.msgbox_new_selected_go_up);         

                lp.gravity = Gravity.RIGHT;     
        }
        //If not mine then it is from sender to show orange background and align to left

        else        
        {
            holder.message.setBackgroundResource(R.drawable.msgbox_other_go_up);

            lp.gravity = Gravity.LEFT;

        }
            holder.message.setLayoutParams(lp);
            //holder.message.setTextColor(R.color.textColor);   

        return convertView;
    }
    private static class ViewHolder
    {
        TextView message;
    }

    @Override
    public long getItemId(int position) {
        //Unimplemented, because we aren't using Sqlite.
        return position;
    }

}
import java.util.ArrayList;
导入android.content.Context;
导入android.util.Log;
导入android.view.Gravity;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.BaseAdapter;
导入android.widget.LinearLayout.LayoutParams;
进口