ArrayAdapter实例在onCreate之外变为null

时间:2015-11-13 05:47:43

标签: android listener android-arrayadapter

我正在ArrayAdapter中创建onCreate()的实例。能够在按钮OnClick()中使用该实例。

@Override
public void onCreate(Bundle savedInstanceState) {
    StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork().penaltyLog().build());
    StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectLeakedSqlLiteObjects().detectLeakedClosableObjects().penaltyLog().penaltyDeath().build());

    super.onCreate(savedInstanceState);
    setContentView(R.layout.chat_layout);

    chatBox = (EditText)findViewById(R.id.chat_box);
    chatsListView= (ListView) findViewById(R.id.chat_listView);
    sendbtn= (ImageButton) findViewById(R.id.send);
    attachIcon = (ImageView) findViewById(R.id.add);
    addSmileyIcon = (ImageView) findViewById(R.id.smileys);

    selectedUser = getIntent().getStringExtra("selectedUser");
    userName =  SharedUtils.getSharedPreferences("name", ChattingActivity.this);
    currentDate = ConnectionUtility.getCurrentDate();
    chatMessageList = new ArrayList<ChatMessage>();

    ActionBar actionBar = getSupportActionBar();
    actionBar.setBackgroundDrawable(getResources().getDrawable(R.color.actionbar_color));
    actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
    View cView = getLayoutInflater().inflate(R.layout.actionbar_layout, null);
    actionBar.setCustomView(cView);
    ActionBar.LayoutParams layoutParams = new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT);
    actionBar.setCustomView(cView, layoutParams);
    Toolbar parent = (Toolbar) cView.getParent();
    parent.setContentInsetsAbsolute(0, 0);

    icon1 = (ImageView) cView.findViewById(R.id.icon1);
    icon2 = (ImageView) cView.findViewById(R.id.icon2);
    backIcon= (ImageView) cView.findViewById(R.id.backbutton);

    icon1.setImageResource(R.drawable.call);
    icon2.setImageResource(R.drawable.videocall);
    backIcon.setImageResource(R.drawable.back_arrow);

    chatArrayAdapter = new ChatArrayAdapter(ChattingActivity.this, R.layout.activity_chat_singlemessage);
    chatsListView.setAdapter(chatArrayAdapter);
    chatsListView.setTranscriptMode(AbsListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
    chatArrayAdapter.registerDataSetObserver(new DataSetObserver() {
        @Override
        public void onChanged() {
            super.onChanged();
            chatsListView.setSelection(chatArrayAdapter.getCount() - 1);
        }
    });

    attachIcon.setOnClickListener(this);
    backIcon.setOnClickListener(this);
    addSmileyIcon.setOnClickListener(this);
    sendbtn.setOnClickListener(this);
    chatBox.setOnEditorActionListener(this);

    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            DatabaseHelper dbHandler  = new DatabaseHelper(ChattingActivity.this);
            dbHandler.getChat(userName);
        }
    },1000);
}

以下方法属于同一活动,并通过listener调用。在此,它将ArrayAdapter实例显示为null

@Override
public void onReceivedMessage(JSONObject messageObj) {
    Log.d(":::::::::::::::::::::","Chatting");
    isIncomingMessage =true;
    try {
        final  String   message = messageObj.getString("msg");
        final  String  from = messageObj.getString("from");
        final  String   to = messageObj.getString("to");
        final  String time = messageObj.getString("timestamp");
        final  String type = messageObj.getString("type");
        final  boolean  isFileTransfer = messageObj.getBoolean("isFileTransfer");
        final  boolean isMedia = messageObj.getBoolean("isMedia");
        final  boolean isAudio = messageObj.getBoolean("isAudio");
        final  boolean  isVideo = messageObj.getBoolean("isVideo");
        final  String id= messageObj.getString("id");
        final  boolean  isSent=messageObj.getBoolean("isSent");
        final  boolean isDelivered = messageObj.getBoolean("isDelivered");
        final  boolean isRead = messageObj.getBoolean("isRead");

        sendMessageAck(messageObj);

        ChatMessage obj = new ChatMessage(id,message,from,time,isSent,isDelivered , isRead,type,isVideo,isAudio,isMedia,isFileTransfer,isIncomingMessage);
        Log.d("ArrayAdapter instance",""+chatArrayAdapter);
        //chatArrayAdapter.notifyDataSetChanged();
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

由于 维奈

0 个答案:

没有答案