简体   繁体   中英

Starting another activity - application crash

At the beginning I notice that I'm novice at Android development.

I made application from http://developer.android.com/training/basics/firstapp/starting-activity.html and it worked well (second activity too). I want to make small change: in second activity I want to display "Your message:" text and below message from input from first activity. I did it my way and now when I tap Send button I have an application crash.

My second activity xml code:

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.lislav.firstandroidapp.DisplayMessageActivity">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/header_my_message"
    android:textSize="40px"/>
<TextView
    android:id="@+id/message"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="70px"/>
</RelativeLayout>

and second activity java code:

package com.example.lislav.firstandroidapp;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class DisplayMessageActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intent intent = getIntent();
    String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

    TextView textViewMessage = (TextView) findViewById(R.id.message);
    textViewMessage.setText(message);

    setContentView(R.layout.activity_display_message);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

I think it's something wrong with onCreate function.. But I don't know what. Sorry, if there are any obvious mistakes..

You must call findViewById method only after calling setContentView . Just put setContentView right after calling of super.onCreate .

By the way read about logcat tool. This is logger which you should use always facing any problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM