为什么我的Andr​​oid活动与&QUOT崩溃;无适配器连接;跳绳布局"但偶尔? [英] Why my android activity is crashing with "No adapter attached; skipping layout" but sporadically?

查看:308
本文介绍了为什么我的Andr​​oid活动与&QUOT崩溃;无适配器连接;跳绳布局"但偶尔?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个Android应用程序,这是一个消息应用程序实际。

我用 RecyclerView 来渲染一个特定的谈话内容。

我什么套牢的是,我有一个接受用户输入来创建一个新的对话片段,我用这些信息来推出一个活动的谈话,以更新其布局,适配器的RecyclerView等。

我解雇片段,当用户输入有效(非空等),在谈话中发送一封测试邮件,使用会话标识符来启动对话的活动。

但我得到这个 NullPointerException异常这是关系到 RecyclerView ,堆栈跟踪的标题是


  

显示java.lang.NullPointerException:尝试调用虚方法'布尔android.support.v7.widget.RecyclerView$LayoutManager.onAddFocusables(android.support.v7.widget.RecyclerView, java.util.ArrayList中,INT,在一个空对象引用INT)


在此之上,我也得到一个说法无适配器连接;跳绳布局

我看到这里的答案计算器上说,你应该启动一个适配器/ LayoutManager的第一,然后将其连接到RecyclerView,但我已经在做了。

我在这里写我的code的片段。


  

接口方法,当用户输入有效的被调用。


 公共无效onCreateConversation(DialogFragment对话框,字符串CONVERSATIONNAME){    dialog.dismiss();    对话newConversation = client.newConversation(Arrays.asList(用户列表);
    字符串标识符= newConversation.getId()的toString()。    // prepare发起消息
    字符串initialMessageText =开始交谈;
    MessagePart initialMessagePart = client.newMessagePart(文/启动,initialMessageText.getBytes());
    消息initialMessage = client.newMessage(Arrays.asList(initialMessagePart));    //发送发起消息
    newConversation.send(ini​​tialMessage);    startConversationActivity(标识);
}


  

有关谈话开始的活动


 公共无效startConversationActivity(字符串标识符){    意向意图=新意图(这一点,ChatConversationActivity.class);
    intent.putExtra(标记,标识符);    startActivity(意向);
}


  

ChatConversationActivity的onCreate方法


 保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.loading_conversation);    意向意图= getIntent();
    conversationIdentifier = intent.getStringExtra(标记);    //参数客户是参照连接
    conversationViewControl =新ConversationViewController(这一点,客户端,conversationIdentifier);
}


  

在ConversationViewController的构造


 公共ConversationViewController(ChatConversationActivity活动,Client客户端,字符串标识符){
    activity.setContentView(R.layout.activity_conversation);    //消息回收视图
    messageRecyclerView =(RecyclerView)activity.findViewById(R.id.messageRecyclerView);    //为回收视图布局管理器
    recyclerViewLayoutManager =新LinearLayoutManager(活动);    //消息适配器
    MessageAdapter = NULL;    //在ConversationViewController私人谈话对象
    activeConversation = getConversation(标识);    //这会使谈话布局
    drawConversationView(activeConversation);
}


  

getConversation询问谈话的服务与标识符


 私人谈话getConversation(字符串标识符){    如果(activeConversation == NULL){
        查询查询= Query.builder(Conversation.class)
                。predicate(新predicate(Conversation.Property.ID,predicate.Operator.EQUAL_TO,标识符))
                。建立();        清单<会话与GT;结果= client.executeQuery(查询,Query.ResultType.OBJECTS);
        如果(结果=空&放大器;!&放大器; results.size()大于0){            //加载第一对象标识符是为所有对话独特
            返回results.get(0);
        }    }    返回activeConversation;
}


  

drawConversationView将更新会话视图


 私人无效drawConversation(会话会话){    //只继续进行,如果有一个有效的对话
    如果(对话!= NULL){
        Log.d(创造的谈话,activeConversation不为空);        //回收视图布局管理器
        recyclerViewLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        messageRecyclerView.setLayoutManager(recyclerViewLayoutManager);        对于谈话//消息
        清单<消息>消息= client.getMessages(会话);        //回收观点适配器,活动也是私有对象
        messageAdapter =新MessageAdapter(的消息,活动);
        messageRecyclerView.setAdapter(messageAdapter);
    }其他{
        Log.d(创造的谈话,activeConversation仍然是空);
    }
}

这件事崩溃随机,有时谈话被创建,我能看到它的看法,有时没有。

我在Java和Android世界的新,你可以请帮我跟踪此?


解决方案

我可以看到你的code两个不合理的事情。


  

只要你收集到RecyclerView参考附上的LayoutManager


您可以尝试安装在的LayoutManager RecyclerView ConversationViewController 本身,你做任何试图收集您的通话数据之前。

  ...
    //消息回收视图
    messageRecyclerView =(RecyclerView)mca.findViewById(R.id.messageRecyclerView);    //为回收视图布局管理器
    recyclerViewLayoutManager =新LinearLayoutManager(MCA);
    recyclerViewLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);    //设置这个就在这里
    messageRecyclerView.setLayoutManager(recyclerViewLayoutManager);
    ...

这将可能崩溃救你。


  

实现回调,使当你有数据确保你只渲染


ConversationViewController ,您呼叫 getConversation drawConversation 方法此起彼伏,但你在第一个收集了一些数据,并在第二个方法使用它。

您可以尝试实现一个接口,并用它来调用 drawConversation 从方法 getConversation ,一旦你有会话数据,而不是叫他们同步。

我希望这将帮助你在这里。

I'm working on an android application, that is a messaging application actually.

I'm using RecyclerView to render content of a particular conversation.

What I'm stuck with is, I have a fragment that takes input from user to create a new conversation, I use that information to launch an activity for that conversation, to update its layout, adapter for the RecyclerView etc.

I dismiss the fragment when user input is valid(not empty etc.), send a test message in the conversation and use the conversation identifier to start the conversation activity.

But I'm getting this NullPointerException that is related to the RecyclerView, the heading of the stack trace is :

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.support.v7.widget.RecyclerView$LayoutManager.onAddFocusables(android.support.v7.widget.RecyclerView, java.util.ArrayList, int, int)' on a null object reference

Above this, I also get one saying No adapter attached; skipping layout.

I saw answers here on stackoverflow saying that, you should initiate an Adapter/LayoutManager first and then attach it to the RecyclerView, but I'm already doing it.

I'm writing snippets of my code here.

Interface method that gets invoked when user input is valid.

public void onCreateConversation(DialogFragment dialog, String conversationName) {

    dialog.dismiss();

    Conversation newConversation = client.newConversation(Arrays.asList(userList);
    String identifier = newConversation.getId().toString();

    // prepare initiation message
    String initialMessageText = "starting conversation";
    MessagePart initialMessagePart = client.newMessagePart("text/initiation", initialMessageText.getBytes());
    Message initialMessage = client.newMessage(Arrays.asList(initialMessagePart));

    // send initiation message
    newConversation.send(initialMessage);

    startConversationActivity(identifier);
}

Start an activity for the conversation

public void startConversationActivity(String identifier) {

    Intent intent = new Intent(this, ChatConversationActivity.class);
    intent.putExtra("IDENTIFIER", identifier);

    startActivity(intent);
}

The onCreate method of ChatConversationActivity

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.loading_conversation);

    Intent intent = getIntent();
    conversationIdentifier = intent.getStringExtra("IDENTIFIER");

    // argument 'client' is reference to the connection
    conversationViewControl = new ConversationViewController(this, client, conversationIdentifier);
}

In the constructor of ConversationViewController

public ConversationViewController(ChatConversationActivity activity, Client client, String identifier) {
    activity.setContentView(R.layout.activity_conversation);

    // messages recycler view
    messageRecyclerView = (RecyclerView) activity.findViewById(R.id.messageRecyclerView);

    // layout manager for recycler view
    recyclerViewLayoutManager = new LinearLayoutManager(activity);

    // message adapter
    MessageAdapter = null;

    // private conversation object in ConversationViewController
    activeConversation = getConversation(identifier);

    // this will render the layout for conversation
    drawConversationView(activeConversation);
}

'getConversation' asks the service for conversation with an identifier

private Conversation getConversation(String identifier) {

    if(activeConversation == null) {
        Query query = Query.builder(Conversation.class)
                .predicate(new Predicate(Conversation.Property.ID, Predicate.Operator.EQUAL_TO, identifier))
                .build();

        List<Conversation> results = client.executeQuery(query, Query.ResultType.OBJECTS);
        if(results != null && results.size() > 0) {

            // loading first object as identifiers are unique for all conversations
            return results.get(0);
        }

    }

    return activeConversation;
}

'drawConversationView' will update the view for conversation

private void drawConversation(Conversation conversation) {

    // Only proceed if there is a valid conversation
    if(conversation != null) {
        Log.d("create conversation", "activeConversation is not null");

        // recycler view layout manager
        recyclerViewLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        messageRecyclerView.setLayoutManager(recyclerViewLayoutManager);

        // messages for the conversation
        List<Message> messages = client.getMessages(conversation);

        // recycler view adapter, 'activity' is also private object
        messageAdapter = new MessageAdapter(messages, activity);
        messageRecyclerView.setAdapter(messageAdapter);
    } else {
        Log.d("create conversation", "activeConversation is still null");
    }
}

This thing is crashing randomly, sometimes the conversation gets created, I get to see the view of it, sometimes not.

I'm new in both Java and Android world, can you please help me track this?

解决方案

I can see two unreasonable things in your code.

Attach the LayoutManager as soon as you collect a reference to RecyclerView

You can try attaching the LayoutManager to RecyclerView, in the ConversationViewController itself, before you make any attempt to collect your conversation data.

    ...
    // messages recycler view
    messageRecyclerView = (RecyclerView) mca.findViewById(R.id.messageRecyclerView);

    // layout manager for recycler view
    recyclerViewLayoutManager = new LinearLayoutManager(mca);
    recyclerViewLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);

    // set this right here
    messageRecyclerView.setLayoutManager(recyclerViewLayoutManager);
    ...

This would probably save you from crashing.

Implement callbacks to make sure you render only when you have the data

In the ConversationViewController, you are calling getConversation and drawConversation methods one after another, but you are collecting some data in the first and using it in the second method.

You can try implementing an interface and use that to invoke the drawConversation from the method getConversation, once you have the conversation data, instead of calling them synchronously.

I hope this will help you here.

这篇关于为什么我的Andr​​oid活动与&QUOT崩溃;无适配器连接;跳绳布局&QUOT;但偶尔?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆