Java 为什么我的安卓系统活动会崩溃;无适配器连接;跳过布局“;但是偶尔?

Java 为什么我的安卓系统活动会崩溃;无适配器连接;跳过布局“;但是偶尔?,java,android,android-fragments,android-recyclerview,Java,Android,Android Fragments,Android Recyclerview,我正在开发一个android应用程序,实际上是一个消息传递应用程序 我正在使用RecyclerView呈现特定对话的内容 我一直坚持的是,我有一个片段,它从用户那里获取输入来创建一个新的对话,我使用该信息来启动该对话的活动,更新其布局,为RecyclerView提供适配器,等等 当用户输入有效(不是空的等)时,我关闭片段,在对话中发送测试消息,并使用对话标识符启动对话活动 但是我得到了这个与回收视图相关的NullPointerException,堆栈跟踪的标题是: java.lang.NullP

我正在开发一个android应用程序,实际上是一个消息传递应用程序

我正在使用
RecyclerView
呈现特定对话的内容

我一直坚持的是,我有一个片段,它从用户那里获取输入来创建一个新的对话,我使用该信息来启动该对话的活动,更新其布局,为RecyclerView提供适配器,等等

当用户输入有效(不是空的等)时,我关闭片段,在对话中发送测试消息,并使用对话标识符启动对话活动

但是我得到了这个与回收视图相关的
NullPointerException
,堆栈跟踪的标题是:

java.lang.NullPointerException:尝试对空对象引用调用虚拟方法“boolean android.support.v7.widget.RecyclerView$LayoutManager.onAddFocusables(android.support.v7.widget.RecyclerView,java.util.ArrayList,int,int)”

在这上面,我还听到一句话,
没有连接适配器;跳过布局

我在stackoverflow上看到的答案是,您应该首先启动
适配器/LayoutManager
,然后将其连接到RecyclerView,但我已经在做了

我在这里写我的代码片段

当用户输入有效时调用的接口方法

开始对话的活动

ChatConversationActivity的onCreate方法

在ConversationViewController的构造函数中

“getConversation”请求服务使用标识符进行对话

私人会话getConversation(字符串标识符){
if(activeConversation==null){
Query=Query.builder(Conversation.class)
.predicate(新谓词(Conversation.Property.ID、predicate.Operator.EQUAL_TO、identifier))
.build();
列表结果=client.executeQuery(查询、查询、结果类型、对象);
if(results!=null&&results.size()>0){
//加载第一个对象作为标识符对于所有对话都是唯一的
返回结果。获取(0);
}
}
返回主动对话;
}
“drawConversationView”将更新对话的视图

私人对话(对话){
//仅当存在有效对话时才继续
if(对话!=null){
Log.d(“创建会话”,“activeConversation不为空”);
//回收器视图布局管理器
recyclerViewLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
messageRecyclerView.setLayoutManager(recyclerViewLayoutManager);
//对话信息
List messages=client.getMessages(对话);
//回收器视图适配器“活动”也是私有对象
messageAdapter=新messageAdapter(消息、活动);
messageRecyclerView.setAdapter(messageAdapter);
}否则{
Log.d(“创建会话”,“activeConversation仍然为空”);
}
}
这东西正在随机崩溃
,有时会创建对话,我会看到它的视图,有时不会。


我对Java和Android都是新手,你能帮我跟踪一下吗?

我在你的代码中看到两个不合理的地方

收集对RecyclerView的引用后,立即附加LayoutManager

在尝试收集对话数据之前,您可以尝试将
LayoutManager
附加到
RecyclerView
中的
ConversationViewController
本身

    ...
    // 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);
    ...
这可能会使你免于崩溃

实现回调以确保仅在拥有数据时渲染

ConversationViewController
中,您一个接一个地调用
getConversation
drawConversation
方法,但是您在第一个方法中收集一些数据,在第二个方法中使用它

您可以尝试实现一个接口,并使用该接口从方法
getConversation
调用
drawConversation
,一旦您拥有了会话数据,而不是同步调用它们


我希望这将对您有所帮助。

谢谢,这修复了错误。虽然按照您的建议通过正确附加LayoutManager修复了崩溃错误,但它没有加载对话数据。您是否正确实现了基于回调的数据收集/呈现?它应该解决第二个问题。
public void startConversationActivity(String identifier) {

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

    startActivity(intent);
}
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);
}
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);
}
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;
}
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");
    }
}
    ...
    // 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);
    ...