简体   繁体   中英

Android: Application has stopped unexpectedly

I'm a newbie.I was trying to take input from an Edittext in a string and show it in another edittext.The application works fine at start but when i insert the code below i start getting the error "Application stopped Unexpectedly" and after the error when i remove this code i still get the same error.I am totally confused.

Here is my complete class code:

 public class IOActivity extends Activity {
/** Called when the activity is first created. */
 Button button=(Button) findViewById(R.id.button1);
EditText edtxt1=(EditText) findViewById(R.id.editText1);
EditText edtxt2=(EditText) findViewById(R.id.editText2);


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
button.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
String a=edtxt1.getText().toString();   
    edtxt2.setText(a);
    }
});


}
}

My Android.Manifest.xml:

<?xml version="1.0" encoding="utf-8"?>
  <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.example.IO"
  android:versionCode="1"
  android:versionName="1.0">
  <uses-sdk android:minSdkVersion="9" />

   <application android:icon="@drawable/icon" android:label="@string/app_name">

    <activity android:name=".IOActivity" android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

</application>
</manifest>

My Logcat shows the following Errors:

03-19 18:30:17.938: ERROR/Zygote(32): setreuid() failed. errno: 2

03-19 18:30:29.179: ERROR/Zygote(32): setreuid() failed. errno: 17

03-19 18:30:30.868: ERROR/BatteryService(60): usbOnlinePath not found

03-19 18:30:30.868: ERROR/BatteryService(60): batteryVoltagePath not found

03-19 18:30:30.868: ERROR/BatteryService(60): batteryTemperaturePath not found

03-19 18:30:30.888: ERROR/SurfaceFlinger(60): Couldn't open /sys/power/wait_for_fb_sleep or /sys/power/wait_for_fb_wake

03-19 18:30:31.128: ERROR/SensorService(60): couldn't open device for module sensors (Invalid argument)


03-19 18:30:40.158: ERROR/System(60): Failure starting core service

03-19 18:30:40.158: ERROR/System(60): java.lang.SecurityException


03-19 18:30:40.158: ERROR/System(60):     at android.os.BinderProxy.transact(Native Method)

03-19 18:30:40.158: ERROR/System(60):     at android.os.ServiceManagerProxy.addService(ServiceManagerNative.java:146)

03-19 18:30:40.158: ERROR/System(60):     at android.os.ServiceManager.addService(ServiceManager.java:72)

03-19 18:30:40.158: ERROR/System(60):     at com.android.server.ServerThread.run(SystemServer.java:206)

03-19 18:30:40.188: ERROR/EventHub(60): could not get driver version for /dev/input/mouse0, Not a typewriter

03-19 18:30:40.188: ERROR/EventHub(60): could not get driver version for /dev/input/mice, Not a typewriter

03-19 18:30:40.738: ERROR/SoundPool(60): error loading /system/media/audio/ui/Effect_Tick.ogg

03-19 18:30:40.748: ERROR/SoundPool(60): error loading /system/media/audio/ui/KeypressStandard.ogg

03-19 18:30:40.748: ERROR/SoundPool(60): error loading /system/media/audio/ui/KeypressSpacebar.ogg

03-19 18:30:40.748: ERROR/SoundPool(60): error loading /system/media/audio/ui/KeypressDelete.ogg

03-19 18:30:40.758: ERROR/SoundPool(60): error loading /system/media/audio/ui/KeypressReturn.ogg

03-19 18:30:40.808: ERROR/UsbObserver(60): java.lang.NullPointerException

03-19 18:30:40.808: ERROR/UsbObserver(60):     at 
com.android.server.UsbObserver.init(UsbObserver.java:131)

03-19 18:30:40.808: ERROR/UsbObserver(60):     at com.android.server.UsbObserver.<init>(UsbObserver.java:65)

03-19 18:30:40.808: ERROR/UsbObserver(60):     at com.android.server.ServerThread.run(SystemServer.java:402)

03-19 18:30:41.748: ERROR/ThrottleService(60): Could not open GPS configuration file /etc/gps.conf

03-19 18:30:41.998: ERROR/logwrapper(132): executing /system/bin/tc failed: No such file or directory

03-19 18:30:42.070: ERROR/logwrapper(134): executing /system/bin/tc failed: No such file or directory

03-19 18:30:42.118: ERROR/logwrapper(135): executing /system/bin/tc failed: No such file or directory

03-19 18:31:06.573: ERROR/MediaProvider(257): Illegal update request: can't downgrade from 305 to 92. Did you forget to wipe data?

03-19 18:31:06.823: ERROR/MediaScannerService(257): exception in MediaScanner.scan()

03-19 18:31:06.823: ERROR/MediaScannerService(257): java.lang.IllegalArgumentException

03-19 18:31:06.823: ERROR/MediaScannerService(257):     at com.android.providers.media.MediaProvider.updateDatabase(MediaProvider.java:379)

03-19 18:31:06.823: ERROR/MediaScannerService(257):     at com.android.providers.media.MediaProvider.access$200(MediaProvider.java:83)

03-19 18:31:06.823: ERROR/MediaScannerService(257):     at com.android.providers.media.MediaProvider$DatabaseHelper.onUpgrade(MediaProvider.java:201)

03-19 18:31:06.823: ERROR/MediaScannerService(257):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:132)

03-19 18:31:06.823: ERROR/MediaScannerService(257):     at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:187)

03-19 18:31:06.823: ERROR/MediaScannerService(257):     at com.android.providers.media.MediaProvider.query(MediaProvider.java:1290)

03-19 18:31:06.823: ERROR/MediaScannerService(257):     at android.content.ContentProvider$Transport.query(ContentProvider.java:187)

03-19 18:31:06.823: ERROR/MediaScannerService(257):     at android.media.MediaScanner.prescan(MediaScanner.java:911)

03-19 18:31:06.823: ERROR/MediaScannerService(257):     at android.media.MediaScanner.scanDirectories(MediaScanner.java:1184)

03-19 18:31:06.823: ERROR/MediaScannerService(257):     at com.android.providers.media.MediaScannerService.scan(MediaScannerService.java:111)

03-19 18:31:06.823: ERROR/MediaScannerService(257):     at com.android.providers.media.MediaScannerService.access$200(MediaScannerService.java:48)

03-19 18:31:06.823: ERROR/MediaScannerService(257):     at com.android.providers.media.MediaScannerService$ServiceHandler.handleMessage(MediaScannerSe
rvice.java:267)

03-19 18:31:06.823: ERROR/MediaScannerService(257):     at android.os.Handler.dispatchMessage(Handler.java:99)

03-19 18:31:06.823: ERROR/MediaScannerService(257):     at android.os.Looper.loop(Looper.java:123)

03-19 18:31:06.823: ERROR/MediaScannerService(257):     at com.android.providers.media.MediaScannerService.run(MediaScannerService.java:188)

03-19 18:31:06.823: ERROR/MediaScannerService(257):     at java.lang.Thread.run(Thread.java:1019)

03-19 18:31:14.083: ERROR/AndroidRuntime(332): FATAL EXCEPTION: main

03-19 18:31:14.083: ERROR/AndroidRuntime(332): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.IO/com.example.IO.IOActivity}: java.lang.NullPointerException

03-19 18:31:14.083: ERROR/AndroidRuntime(332):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1544)

03-19 18:31:14.083: ERROR/AndroidRuntime(332):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1638)

03-19 18:31:14.083: ERROR/AndroidRuntime(332):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)

03-19 18:31:14.083: ERROR/AndroidRuntime(332):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:928)

03-19 18:31:14.083: ERROR/AndroidRuntime(332):     at android.os.Handler.dispatchMessage(Handler.java:99)

03-19 18:31:14.083: ERROR/AndroidRuntime(332):     at android.os.Looper.loop(Looper.java:123)

03-19 18:31:14.083: ERROR/AndroidRuntime(332):     at android.app.ActivityThread.main(ActivityThread.java:3647)

03-19 18:31:14.083: ERROR/AndroidRuntime(332):     at java.lang.reflect.Method.invokeNative(Native Method)

03-19 18:31:14.083: ERROR/AndroidRuntime(332):     at java.lang.reflect.Method.invoke(Method.java:507)

03-19 18:31:14.083: ERROR/AndroidRuntime(332):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)

03-19 18:31:14.083: ERROR/AndroidRuntime(332):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)

03-19 18:31:14.083: ERROR/AndroidRuntime(332):     at dalvik.system.NativeStart.main(Native Method)

03-19 18:31:14.083: ERROR/AndroidRuntime(332): Caused by: java.lang.NullPointerException

03-19 18:31:14.083: ERROR/AndroidRuntime(332):     at android.app.Activity.findViewById(Activity.java:1647)

03-19 18:31:14.083: ERROR/AndroidRuntime(332):     at com.example.IO.IOActivity.<init>(IOActivity.java:14)

03-19 18:31:14.083: ERROR/AndroidRuntime(332):     at java.lang.Class.newInstanceImpl(Native Method)

03-19 18:31:14.083: ERROR/AndroidRuntime(332):     at java.lang.Class.newInstance(Class.java:1409)

03-19 18:31:14.083: ERROR/AndroidRuntime(332):     at android.app.Instrumentation.newActivity(Instrumentation.java:1021)

03-19 18:31:14.083: ERROR/AndroidRuntime(332):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1536)

03-19 18:31:14.083: ERROR/AndroidRuntime(332):     ... 11 more
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.IO/com.example.IO.IOActivity}: java.lang.NullPointerException

您忘记了在AndroidManifest.xml声明活动吗?

You are trying to get the button trough

Button myButton = (Button) findViewById(R.id.my_button);

You must make sure you do that AFTER the setContentView call

setContentView(R.layout.layoutcontainingbutton);

so it goes wrong since you are trying to make a call to findViewById before those views are loaded.

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