Monday, May 14, 2012

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



public class DisplayMessageActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}
}

public void sendMessage(View view) {
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.





No comments:

Post a Comment