Brian Ecker
Brian Ecker

Reputation: 2077

startActivity() causing crash in Android

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

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);
    }

}

Now I know that won't do anything yet, but why is it crashing? My XML:

<?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>

My manifest:

<?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=".DisplayMessageActivity" >
    </activity>

</application>
</manifest>

I saw that a lot of other people were having problems with this because they had not declared their activities in the manifest, but I think I have done so properly. Any help would be so greatly appreciated.

Upvotes: 1

Views: 2932

Answers (6)

Imran Rana
Imran Rana

Reputation: 11889

Use separate files for TestActivity and DisplayMessageActivity classes. In your given code you are not specifying a layout for your DisplayMessageActivity activity using setContentView.

Upvotes: 0

Dheeresh Singh
Dheeresh Singh

Reputation: 15701

Actullay you have created the Activity inside activity. Which is not much pref-able but if want to stick with that then you have to change the android:name=".DisplayMessageActivity" to

Upvotes: 0

Amit
Amit

Reputation: 13364

I can't understand what you are actually trying to achieve by starting the same activity again. Try this,

    public class DisplayMessageActivity extends Activity {
    private  String message ;
        @Override
        public void onCreate(Bundle savedInstanceState){
            super.onCreate(savedInstanceState);
setLayout(R.layout.<name of the xml file> ); // change the name if it is not in layout folder
           EditText editText = (EditText) findViewById(R.id.textBox1);
           String message = editText.getText().toString();
           Button submitBtn = (Button) findViewById(R.id.button1);
           submitBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                     Toast toast = Toast.makeText(DisplayMessageActivity.this,
                                message,
                                Toast.LENGTH_SHORT);
                        toast.show();
    }});
        }
    }

The above code simply displays the String, This is just a sample code... Hope that helps!!!

EDIT To run this code you must also add id tag to your Button Tag inside XML file having value button1..

Upvotes: 0

Wampie Driessen
Wampie Driessen

Reputation: 1710

the problem is this,
you declare in the manifest that the main class is TestActivity:

<activity
    android:name=".TestActivity"
    android:label="@string/app_name" >

but you wanted to start DisplayMessageActivity, so, change this to the following:

public class TestActivity extends Activity {
@Override

Upvotes: 0

ccheneson
ccheneson

Reputation: 49410

One possible problem is that

EditText editText = (EditText) findViewById(R.id.textBox1);

returns null because you dont set anywhere the layout for your activity (with setContentView)

If your editetext is included in view (passed to sendMessage), you can find it with

EditText editText = (EditText) view.findViewById(R.id.textBox1);

Upvotes: 2

Master Chief
Master Chief

Reputation: 2541

I think problem is you are on DisplayMessageActivity and starting the same activity. What you need to do is start test activity and call DisplayMessageActivity from intent.

Upvotes: 1

Related Questions