When I launch application MainActivity launches successfully but when I insert text in the text edit field and I press button application crashes

Android Manifest File

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.sateesh.explicit_intents.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.sateesh.explicit_intents.Second"
                  android:label="@string/app_name">

        </activity>            
      </application>

</manifest>

This is the Main_Activity:

    package com.sateesh.explicit_intents;

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

    public class MainActivity extends Activity {

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

                // Refernce of UI objects
            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 i= new Intent(MainActivity.this, Second.class);

                                // Getting 
                    i.putExtra("thetext", et.getText().toString());
                    startActivity(i);

                }
            });
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }

    }

main_activity.xml:

<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

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

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText1"
        android:layout_below="@+id/editText1"
        android:text="Go To Second Activity" />

</RelativeLayout>

Second Activity:

    package com.sateesh.explicit_intents;

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

    public class Second extends Activity {
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            TextView tv=(TextView)findViewById(R.id.textView1);

            tv.setText(getIntent().getExtras().getString("thetext"));
        }


    }

second.xml:

    <?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="TextView" />

</LinearLayout>

When I try to run this on emulator. MainActivity launches successfully. But, when I insert text and press the button program crashes and I get following errors.

        10-28 00:43:16.692: D/gralloc_goldfish(2002): Emulator without GPU emulation detected.

        10-28 00:43:17.462: W/KeyCharacterMap(2002): No keyboard for id 0

        10-28 00:43:17.462: W/KeyCharacterMap(2002): Using default keymap: /system/usr/keychars/qwerty.kcm.bin

        10-28 00:43:18.393: D/AndroidRuntime(2002): Shutting down VM

        10-28 00:43:18.393: W/dalvikvm(2002): threadid=1: thread exiting with uncaught exception (group=0xb6f794f0)

        10-28 00:43:18.433: E/AndroidRuntime(2002): FATAL EXCEPTION: main

        10-28 00:43:18.433: E/AndroidRuntime(2002): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sateesh.explicit_intents/com.sateesh.explicit_intents.Second}: java.lang.NullPointerException

        10-28 00:43:18.433: E/AndroidRuntime(2002):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)

        10-28 00:43:18.433: E/AndroidRuntime(2002):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)

        10-28 00:43:18.433: E/AndroidRuntime(2002):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)

        10-28 00:43:18.433: E/AndroidRuntime(2002):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)

        10-28 00:43:18.433: E/AndroidRuntime(2002):     at android.os.Handler.dispatchMessage(Handler.java:99)

        10-28 00:43:18.433: E/AndroidRuntime(2002):     at android.os.Looper.loop(Looper.java:130)

        10-28 00:43:18.433: E/AndroidRuntime(2002):     at android.app.ActivityThread.main(ActivityThread.java:3683)

        10-28 00:43:18.433: E/AndroidRuntime(2002):     at java.lang.reflect.Method.invokeNative(Native Method)

        10-28 00:43:18.433: E/AndroidRuntime(2002):     at java.lang.reflect.Method.invoke(Method.java:507)

        10-28 00:43:18.433: E/AndroidRuntime(2002):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)

        10-28 00:43:18.433: E/AndroidRuntime(2002):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)

        10-28 00:43:18.433: E/AndroidRuntime(2002):     at dalvik.system.NativeStart.main(Native Method)

        10-28 00:43:18.433: E/AndroidRuntime(2002): Caused by: java.lang.NullPointerException

        10-28 00:43:18.433: E/AndroidRuntime(2002):     at com.sateesh.explicit_intents.Second.onCreate(Second.java:14)

        10-28 00:43:18.433: E/AndroidRuntime(2002):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)

        10-28 00:43:18.433: E/AndroidRuntime(2002):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)

        10-28 00:43:18.433: E/AndroidRuntime(2002):     ... 11 more

        10-28 00:43:20.711: I/Process(2002): Sending signal. PID: 2002 SIG: 9

When I launch application MainActivity launches successfully but when I insert text in the text edit field and I press button application crashes.

有帮助吗?

解决方案

Change this line:

setContentView(R.layout.activity_main);

in your Second Activity to:

setContentView(R.layout.second);

There is no TextView in your activity_main.xml layout with the id textView1. There however is in your second.xml layout. And since your Activity is called Second, I'll just make a wild guess here and say, that you set the wrong layout to your Second Activity.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top