简体   繁体   中英

Application stops unexpectedly - Android

I am new to android development. After setting up an android project, I tried to get text input and passe it to another activity (screen).

When I run the project I don't get an error but when I click the application it shows the first screen then when click the button in the screen it gives the error 'application stopped unexpectedly'.

When I try the code without passing data from first screen to second screen , the application works properly.

This is MainActivity.java file:

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

public class MainActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final EditText et= (EditText) findViewById(R.id.editText1);
        Button b = (Button)findViewById(R.id.button1);

        b.setOnClickListener(new OnClickListener() {


            public void onClick(View v) {
        //intent class is used for activating another or component or an activity
            Intent intent =new Intent(MainActivity.this, Second.class);
                intent.putExtra("textval", et.getText().toString());
                startActivity(intent);
            }
        });}}

Here is the code for Second.java file:

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

public class Second extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    TextView tv= (TextView) findViewById(R.id.textView1);
    tv.setText(getIntent().getExtras().getString("textval"));


}
}

Here is the activitymain.xml code:

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

    <EditText android:id="@+id/editText1" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true" android:layout_alignParentTop="true"
        android:ems="10"
        android:inputType="text"
        >

        <requestFocus />
    </EditText>

    <Button android:id="@+id/button1" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:layout_alignParentLeft="true"
        android:layout_below="@+id/editText1" android:layout_marginTop="28dp"
        android:text="@string/button" />

</RelativeLayout>

Here is the second xml file:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView android:id="@+id/textView1" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:text="@string/textview" />

</LinearLayout>

Please help me to find the error. Since I don't get an error notification I am not able to proceed.

Thanks in advance...

Declare Second Activity in android manifest file .

<activity android:name=".Second"/>

Add setContentView(R.layout.second); in second activity.

This is what you missed on your Second.java :

setContentView(R.layout.second);

put it above of your textview declaration.

In your Second java file make changes as follows

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

public class Second extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.second);

    TextView tv= (TextView) findViewById(R.id.textView1);
    tv.setText(getIntent().getExtras().getString("textval"));


}
}

Add entry for second activity in your Manifest file. Put first activity as a Launcher and other as a Default. Here I have two activity MainActivity and Player. First I Launches MainActivity and then call other activity Player

Note- Activity name should be same as class name, so Keep same in Manifest file as well.

Also you need to specify the layout file for both activity by setContentView in onCreate function.

Here is sample code-

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.vt.soc"
    android:versionCode="1"
    android:versionName="1.0" >

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

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Player"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="com.vt.soc.PALYER" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest>

- In your Second Activity you forgot to add the setContentView() .

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.second);  

    TextView tv= (TextView) findViewById(R.id.textView1);
    tv.setText(getIntent().getExtras().getString("textval"));


}

- Please also do see that you have added this Second Activity in your Manifest.xml file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM