-2

This is AcceptCahatActivity file for Chat App
this is example from backendless.com. It is compilin without errors. But this app is closing after starting. "Unfortunately app has stopped"

 package com.backendless.chat.messaging;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.backendless.Backendless;
import com.backendless.services.messaging.MessageStatus;
import com.backendless.messaging.PublishOptions;
import com.backendless.services.messaging.PublishStatusEnum;

public class AcceptChatActivity extends Activity
{
private Button acceptButton, declineButton;
private TextView chatInvitationTextView;

private PublishOptions publishOptions;
private String subtopic;
private String companionNickname;

@Override
public void onCreate( Bundle savedInstanceState )
{
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_accept_chat);

Intent currentIntent = getIntent();
subtopic = currentIntent.getStringExtra( "subtopic" );
companionNickname = subtopic.split( "_" )[ 0 ];

initUI();

Backendless.setUrl( Defaults.SERVER_URL );
Backendless.initApp( this, Defaults.APPLICATION_ID, Defaults.SECRET_KEY, Defaults.VERSION );

publishOptions = new PublishOptions();
publishOptions.setSubtopic( subtopic );
}

private void initUI()
{
acceptButton = (Button) findViewById( R.id.acceptButton );
declineButton = (Button) findViewById( R.id.declineButton );
chatInvitationTextView = (TextView) findViewById( R.id.chatInvitationText );

chatInvitationTextView.setText( String.format( getResources().getString( R.string.chat_invitation_message ), companionNickname ) );


 acceptButton.setOnClickListener( new View.OnClickListener()
 {
  @Override
  public void onClick( View v )
  {
    onAccept();
  }
  } );

declineButton.setOnClickListener( new View.OnClickListener()
{
  @Override
  public void onClick( View v )
  {
    onDecline();
  }
 } );
}

 private void onAccept()
{
  Intent nextIntent = new Intent( this, ChooseNicknameActivity.class );
 nextIntent.putExtra( "owner", false );
 nextIntent.putExtra( "subtopic", subtopic );
nextIntent.putExtra( "companionNickname", companionNickname );
startActivity( nextIntent );
finish();
} 

 private void onDecline()
 {
 Backendless.Messaging.publish( (Object)    
DefaultMessages.DECLINE_CHAT_MESSAGE, publishOptions, new       
DefaultCallback<MessageStatus>( this )
 {
  @Override
  public void handleResponse( MessageStatus response )
  {
    super.handleResponse( response );
    if( response.getStatus() == PublishStatusEnum.SCHEDULED )
    {
      finish();
    }
    else
    {
      Toast.makeText( AcceptChatActivity.this, response.getStatus().toString(), Toast.LENGTH_SHORT ).show();
    }
  }
} );
}
}

This is AcceptCahatActivity file for Chat App
this is example from backendless.com. It is compilin without errors. But this app is closing after starting. "Unfortunately app has stopped" And this Log informaTION

  E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.backendless.chat.messaging, PID: 2770
              java.lang.RuntimeException: Unable to start activity   ComponentInfo{com.backendless.chat.messaging/com.backendless.chat.messaging.AcceptChatActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String[] java.lang.String.split(java.lang.String)' on a null object reference
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
                  at android.app.ActivityThread.access$800(ActivityThread.java:151)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:135)
                  at android.app.ActivityThread.main(ActivityThread.java:5254)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at java.lang.reflect.Method.invoke(Method.java:372)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
               Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String[] java.lang.String.split(java.lang.String)' on a null object reference
                  at com.backendless.chat.messaging.AcceptChatActivity.onCreate(AcceptChatActivity.java:32)
                  at android.app.Activity.performCreate(Activity.java:5990)
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
                  at android.app.ActivityThread.access$800(ActivityThread.java:151) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                  at android.os.Looper.loop(Looper.java:135) 
                  at android.app.ActivityThread.main(ActivityThread.java:5254) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at java.lang.reflect.Method.invoke(Method.java:372) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
  I/Process: Sending signal. PID: 2770 SIG: 9
   Disconnected from the target VM, address: 'localhost:8601', transport: 'socket'

activity_accept_chat

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:paddingLeft="@dimen/dp16"
          android:paddingRight="@dimen/dp16"
          android:gravity="center">

<TextView
        android:layout_width="match_parent"
        android:layout_height="@dimen/dp48"
        android:text="@string/chat_invitation_default"
        android:id="@+id/chatInvitationText"
        android:textSize="@dimen/text_large"
        android:layout_marginBottom="@dimen/dp4"
        android:gravity="center"/>

<LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="@dimen/dp48"
        android:layout_marginTop="@dimen/dp4"
        android:gravity="center">

    <Button
            android:layout_width="@dimen/dp144"
            android:layout_height="match_parent"
            android:text="@string/decline_button_text"
            android:id="@+id/declineButton"
            android:layout_marginRight="@dimen/dp4"/>

    <Button
            android:layout_width="@dimen/dp144"
            android:layout_height="match_parent"
            android:text="@string/accept_button_text"
            android:id="@+id/acceptButton"
            android:layout_marginLeft="@dimen/dp4"/>
   </LinearLayout>
  </LinearLayout>
  • 2
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Phantômaxx Oct 21 '16 at 15:41
  • 1
    In the log you can see there was a problem at line 32 of your AcceptChatActivity file: com.backendless.chat.messaging.AcceptChatActivity.onCreate(AcceptChatActivity.java:32). It appears that the subtopic has the value ```null``` – barry Oct 21 '16 at 16:12

1 Answers1

0

Your subtopic is null and your are not null checking. replace

Intent currentIntent = getIntent(); subtopic = currentIntent.getStringExtra( "subtopic" );

by

Bundle bundle = getIntent().getExtras(); subtopic = bundle.getString("subtopic");

or

subtopic = currentIntent.getStringExtra("subtopic") !=null? currentIntent.getStringExtra( "subtopic" ): "";

Juan Felippo
  • 224
  • 2
  • 9