user2882662
user2882662

Reputation: 535

My app stops working

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 problem:

1.When I click on the button, the app stops saying "Unfortunately Write n Display and stopped.", rather than launching the second activity at all.

The Logcat can be found here: enter link description here since adding it to the question exceeded the limit.

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

    public 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:layout_centerHorizontal="true"

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


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


        android:layout_width="wrap_content"
        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_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"

        android:layout_centerHorizontal="true"

        android:text="@string/title_SecondActivity"
        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>

Upvotes: 0

Views: 201

Answers (4)

Yauraw Gadav
Yauraw Gadav

Reputation: 1746

Logcat shows the following:

Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.practice.myfirstapp1/com.practice.myfirstapp1.SecondActivity}; have you declared this activity in your AndroidManifest.xml?

You did define SecondActivity in manifest, but the package name is wrong ( android:name="com.practive.myfirstapp1.SecondActivity" should be ndroid:name="com.practice.myfirstapp1.SecondActivity" ).

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

It should be

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

Upvotes: 0

Shakeeb Ayaz
Shakeeb Ayaz

Reputation: 6096

you can solve your problem like this

1.replace

android:gravity="center_horizontal" by android:layout_gravity="center_horizontal".

2.Give layout_width as wrap_content for edittext of first activity xml

3.Make your sendMessage method public not private

To get the logcat In Eclipse, Goto Window-> Show View -> Other -> Android-> Logcat.

OR

Write LogCat in Quick Access edit box in your eclipse window (top right corner, just before Open Prospective button). And just select LogCat it will open-up the LogCat window in your current prospec

Upvotes: 1

Bhoomika Brahmbhatt
Bhoomika Brahmbhatt

Reputation: 7415

To solve your first issue: put below code to your textview of both xml files.

Reason: You had not put the android:layout_centerHorizontal="true" in your TextView

android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 android:text="@string/title_MainActivity"

To solve your second issue:Change LAYOUT OF FIRST ACTIVITY:

Reason: android:layout_width is "0dp". it should be wrap_content or fill_parent or match_parent

<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" />

To:

<EditText
    android:id="@+id/EditText1_MainActivity" 
    android:layout_width="wrap_content" //you can put, fill_parent or match_parent
    android:layout_height="wrap_content"
    android:layout_below="@+id/TextView1_MainActivity"
    android:hint="@string/EditText_MainActivity"
    android:textStyle="italic" />

To solve your third issue: change your CODE OF FIRST ACTIVITY: -

Reason: You had put editText=(EditText) findViewById(R.id.EditText1_MainActivity); in sendMessage(View view). send message contains a View so whenever you initiate any component in this method, it will search that component on this particular view which is incorrect. you can initiate your components in any method which does not contain any view parameter.if you want to initiate any component in particular view then you have to initiate it like editText=(EditText)view.findViewById(R.id.EditText1_MainActivity);. But in your case edittext is in main view so you need to remove it from sendMessage(View view).

public class MainActivity extends Activity {
    public static final String key_name="com.practice.firstApp.key";
  EditText editText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editText=(EditText) findViewById(R.id.EditText1_MainActivity);
    }

    public void sendMessage(View view){
        Intent intent= new Intent(this, SecondActivity.class);

        String key_value= editText.getText().toString();
        intent.putExtra(key_name, key_value);
        startActivity(intent);
    }

}

Change your second Activity

Reason: TextView textView= new TextView(this); is not correct way to initialize any component of your xml view.

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

To:

    TextView textView;
    textView= (TextView) findViewById(R.id.TextView2_SecondActivity);
    textView.setText(intent_value);

Upvotes: 1

Shijil
Shijil

Reputation: 2246

change private void sendMessage(View view) to public void sendMessage(View view) (private to public)

LAYOUT OF FIRST ACTIVITY:

<LinearLayout 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"
android:gravity="center_vertical" >

 <TextView 
    android:id="@+id/TextView1_MainActivity"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@+string/title_MainActivity"
    android:textStyle="bold"/>


<EditText
    android:id="@+id/EditText1_MainActivity"
     android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:hint="@string/EditText_MainActivity"
    android:textStyle="italic" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/Button_MainActivity"
    android:onClick="sendMessage"/>

LAYOUT OF SECOND ACTIVITY: -

<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" 
android:gravity="center_vertical"
tools:context=".SecondActivity">

<TextView 
    android:layout_alignParentTop="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@+string/title_SecondActivity"
    android:textStyle="bold"/>
<TextView
    android:id="@+id/TextView2_SecondActivity"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

Upvotes: 0

Related Questions