0

I'm following Google's android dev tutorial. I'm at the very beginning but I've run into a problem and can't seem to solve it. http://developer.android.com/training/basics/firstapp/starting-activity.html#ReceiveIntent

Whenever I click the "Send" button, my app crashes instead of opening the new activity.

The XML for my initial home activity:

</LinearLayout>
    <EditText
    android:id="@+id/edit_message"
    android:layout_weight="1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:hint="@string/edit_message" />

    <Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/button_send"
    android:onClick="sendMessage"/>
</LinearLayout>

The sendMessage method in the Home.Java file

public void sendMessage(View view){
    Intent intent = new Intent(this, DisplayMessageActivity.class);
    EditText editText = (EditText) findViewById(R.id.edit_message);
    String message = editText.getText().toString();
    intent.putExtra(EXTRA_MESSAGE, message);
    startActivity(intent);
}

And the Java for the new activity

    //Get the message from the intent
    Intent intent = getIntent();
    String message = intent.getStringExtra(Home.EXTRA_MESSAGE);

    //Creating the textview
    TextView textView = new TextView(this);
    textView.setTextSize(40);
    textView.setText(message);

    //Set text view as activity layout
    setContentView(textView);

The Main error:

java.lang.RuntimeException: Unable to start activity ComponentInfo{appfactree.me.dotnotes/appfactree.me.dotnotes.DisplayMessageActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.support.v7.widget.Toolbar.getTitle()' on a null object reference

Please ask if you need anything else. Thanks a lot for any help.

EDIT: As requested the full DisplayMessageActivity.java code:

public class DisplayMessageActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    //Get the message from the intent
    Intent intent = getIntent();
    String message = intent.getStringExtra(Home.EXTRA_MESSAGE);

    //Creating the textview
    TextView textView = new TextView(this);
    textView.setTextSize(40);
    textView.setText(message);

    //Set text view as activity layout
    setContentView(textView);
    }
}
als26
  • 91
  • 9

1 Answers1

0

Your LinearLayout is slight bung ... you have a closing tag that opens the LinearLayout. User instead of an opening tag on the first line.

This is not the cause of the issue. It appears to be something to do with you toolbar ... however cannot decipher exactly what from the code you have posted.

fergdev
  • 963
  • 1
  • 13
  • 27