0

当我启动应用程序时 MainActivity 成功启动但是当我在文本编辑字段中插入文本并按下按钮时应用程序崩溃

Android 清单文件

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

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

第二个活动:

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


    }

第二个.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>

当我尝试在模拟器上运行它时。MainActivity 成功启动。但是,当我插入文本并按下按钮时,程序崩溃并出现以下错误。

        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

当我启动应用程序时 MainActivity 成功启动,但是当我在文本编辑字段中插入文本并按下按钮时应用程序崩溃。

4

1 回答 1

1

更改此行:

setContentView(R.layout.activity_main);

在您的Second活动中:

setContentView(R.layout.second);

TextView您的布局中没有activity_main.xmlid textView1。但是,在您的second.xml布局中。而且由于您的 Activity 被调用Second,我将在这里做一个疯狂的猜测并说,您为您的SecondActivity 设置了错误的布局。

于 2013-10-29T23:17:51.500 回答