• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps startActivity() causing crash

Bbrian

Lurker
So I just started learning to develop Android apps, and I have a programming background (Python mainly), so I somewhat know what I'm doing. I'm having a problem with using startActivity(). I've commented code to figure out exactly where the error gets thrown, and it happens right as startActivity() is encountered. The error I get is from the emulator and it is just a popup window that says, "Unfortunately, Test has stopped." (Test is my program name) after I click my button. My code is this

Code:
package com.test.test;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class TestActivity extends Activity {
	
	public final static String EXTRA_MESSAGE = "com.test.test.MESSAGE";
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    
    public class DisplayMessageActivity extends Activity {
    	@Override
    	public void onCreate(Bundle savedInstanceState){
    		super.onCreate(savedInstanceState);
    	}
    }

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

}

My xml is here
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >
    
    <EditText android:id="@+id/textBox1"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:hint="@string/textBox1Hint" />
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button1"
        android:onClick="sendMessage" />   
    
</LinearLayout>

and my Manifest is here

Code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.test.test"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".TestActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        
        <activity
            android:name="test.test.DisplayMessageActivity" 
            android:label="Message" >
        </activity>
        
    </application>
</manifest>

Any help would be greatly greatly appreciated. I just don't get what is wrong.
 
Are you trying to get the text you sent with putExtra?
If so you should do it like this in your DisplayMessageActivity where you have
Code:
Intent intent = getIntent();
Code:
Bundle extras = getIntent().getExtras();
String message = "";
if (extras != null) {
    message = extras.getString(EXTRA_MESSAGE);
 
Yes, I am trying to do that eventually, but that isn't my problem really. The problem is that my program crashes seemingly without reason when it tries to do startActivity(intent) :confused:

I see that a lot of other people who had this same problem resolved it by adding their activity that they are calling to the manifest, but I believe I have done this correctly.

I might have confused you by not posting my entire code. I just updated it to include everything.
 
it is useful to post LogCat messages when you're asking for help with crashes/ANR etc.

Since you're learning it is good idea to build it into your practice the suggestion made by @cr5315, its just a safety cache technique and a good one, intents arriving from 'neither-world' can be null.

Now you've got to do few of things

1: Move DisplayMessageActivity into file of its own call it DisplayMessageActivity.java and package name on top should be com.test.test;
2: PS: Your DisplayMessageActivity is a empty class i.e. it does nothing after it is created;
3: Change your manifest file to point to .DisplayMessageActivity (assuming com.test.test is your package name resides and your java file layout an manifest 'package=' line follows the same convention).

give it a go and let us know
 
Perfect thanks :) I know it's an empty class. I was just trying to get it to change activities before I made the class do anything. Everything is working great now.
 
Back
Top Bottom