My app has some basic problems, and it stops working

Posted by user2882662 on Stack Overflow See other posts from Stack Overflow or by user2882662
Published on 2013-11-07T12:21:20Z Indexed on 2013/11/08 3:54 UTC
Read the original article Hit count: 364

Filed under:
|

I am writing a basic application which contains two activities. Both contain a TextView showing the title and the first one contains an EditText in which the user types a message and clicks on a button on its side, the second activity is launched which shows the message the user types.

It has the following problems:

  1. The title (the first TextView in both the activities) doesn't show in the middle of the line, despite of the android:gravity="center_horizontal" attribute.
  2. The EditText in the first activity does not show at all.
  3. When I click on the button, the app stops saying "Unfortunately Write n Display and stopped.", rather than launching the second activity at all.

I don't have adequate knowledge about logcat, but I have followed the steps somebody had told me, that is Window>Open Perspective> Other> DDMS Then run the app and select the package name from the Devices and click on log cat, select the exception(s) and export to text file. All contained in the text file is : : E/(): Device disconnected: 1 Since I am not sure of using log cat, so I am posting a screenshot to make clear what I have done.enter image description here

CODE OF FIRST ACTIVITY: -

package com.practice.myfirstapp1;

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

public class MainActivity extends Activity {
    public static final String key_name="com.practice.firstApp.key";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    private void sendMessage(View view){
        Intent intent= new Intent(this, SecondActivity.class);
        EditText editText=(EditText) findViewById(R.id.EditText1_MainActivity);
        String key_value= editText.getText().toString();
        intent.putExtra(key_name, key_value);
        startActivity(intent);
    }

}

LAYOUT OF FIRST ACTIVITY: -

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"

    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    tools:context=".MainActivity" >

     <TextView 
        android:id="@+id/TextView1_MainActivity"
        android:layout_alignParentTop="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:text="@+string/title_MainActivity"
        android:gravity="center_horizontal"
        android:textStyle="bold"/>


    <EditText
        android:id="@+id/EditText1_MainActivity"


        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/TextView1_MainActivity"

        android:hint="@string/EditText_MainActivity"
        android:textStyle="italic" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/TextView1_MainActivity"
        android:layout_toRightOf="@id/EditText1_MainActivity"

        android:text="@string/Button_MainActivity"

        android:onClick="sendMessage"/>

</RelativeLayout>

CODE OF SECOND ACTIVITY: -

package com.practice.myfirstapp1;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

class SecondActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        Intent intent= getIntent();
        String intent_value= intent.getStringExtra(MainActivity.key_name);

        TextView textView= new TextView(this);
        textView= (TextView) findViewById(R.id.TextView2_SecondActivity);

        textView.setText(intent_value);

    }
}

LAYOUT OF SECOND ACTIVITY: -

<?xml version="1.0" encoding="utf-8"?>
<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:orientation="horizontal" 

    tools:context=".SecondActivity">

    <TextView 
        android:layout_alignParentTop="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:text="@+string/title_SecondActivity"
        android:gravity="center_horizontal"
        android:textStyle="bold"/>

    <TextView
        android:id="@+id/TextView2_SecondActivity"

        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

STRINGS RESOURCE FILE:-

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">Write n Display</string>
    <string name="action_settings">Settings</string>

    <string name="title_MainActivity">WRITE</string>
    <string name="EditText_MainActivity">Your Message here</string>

    <string name="Button_MainActivity">Send</string>

    <string name="title_SecondActivity">DISPLAY</string>

</resources>

ANDROID MANIFEST FILE: -

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
        android:debuggable="true" >

        <activity
            android:name="com.practice.myfirstapp1.MainActivity"
            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="com.practive.myfirstapp1.SecondActivity"
            android:label="@string/app_name">
        </activity>
    </application>

</manifest>

© Stack Overflow or respective owner

Related posts about android

Related posts about android-layout