Getting an exception when app starts
我正在编写一个程序,当按下按钮时会调用某人。 但是,每当我启动应用程序时,它甚至会在按下按钮之前崩溃。 这是代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | package com.test; import android.app.Activity; import android.content.ActivityNotFoundException; import android.os.Bundle; import android.widget.*; import android.view.*; import android.view.View.OnClickListener; import android.content.Intent; import android.net.Uri; import android.util.Log; public class MainActivity extends Activity { private OnClickListener mButtonListener = new OnClickListener() { public void onClick(View v) { try { Intent callIntent = new Intent(Intent.ACTION_CALL); callIntent.setData(Uri.parse("tel:123456789")); startActivity(callIntent); } catch (ActivityNotFoundException activityException) { Log.e("Test","Call failed"); } } }; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button button = (Button)findViewById(R.id.button); button.setOnClickListener(mButtonListener); } }; |
这是我的布局:
1 2 3 4 5 6 7 8 | <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:layout_width="wrap_content" android:id="@+id/button" android:layout_height="wrap_content" android:text="@string/callme" /> </LinearLayout> |
这是我得到的错误(来自logcat)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | D/AndroidRuntime( 337): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<< D/AndroidRuntime( 337): CheckJNI is ON D/AndroidRuntime( 337): Calling main entry com.android.commands.am.Am I/ActivityManager( 78): Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.test/.MainActivity } from pid 337 I/ActivityManager( 78): Start proc com.test for activity com.test/.MainActivity: pid=345 uid=10035 gids={1015} D/AndroidRuntime( 337): Shutting down VM I/AndroidRuntime( 337): NOTE: attach of thread 'Binder Thread #3' failed D/dalvikvm( 337): GC_CONCURRENT freed 102K, 69% free 319K/1024K, external 0K/0K, paused 2ms+2ms D/dalvikvm( 337): Debugger has detached; object registry had 1 entries I/ARMAssembler( 78): generated scanline__00000177:03515104_00001002_00000000 [ 87 ipp] (110 ins) at [0x4456d6f0:0x4456d8a8] in 715314 ns D/AndroidRuntime( 345): Shutting down VM W/dalvikvm( 345): threadid=1: thread exiting with uncaught exception (group=0x40015560) E/AndroidRuntime( 345): FATAL EXCEPTION: main E/AndroidRuntime( 345): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test/com.test.MainActivity}: java.lang.ClassCastException: android.widget.TextView E/AndroidRuntime( 345): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647) E/AndroidRuntime( 345): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) E/AndroidRuntime( 345): at android.app.ActivityThread.access$1500(ActivityThread.java:117) E/AndroidRuntime( 345): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) E/AndroidRuntime( 345): at android.os.Handler.dispatchMessage(Handler.java:99) E/AndroidRuntime( 345): at android.os.Looper.loop(Looper.java:123) E/AndroidRuntime( 345): at android.app.ActivityThread.main(ActivityThread.java:3683) E/AndroidRuntime( 345): at java.lang.reflect.Method.invokeNative(Native Method) E/AndroidRuntime( 345): at java.lang.reflect.Method.invoke(Method.java:507) E/AndroidRuntime( 345): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) E/AndroidRuntime( 345): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) E/AndroidRuntime( 345): at dalvik.system.NativeStart.main(Native Method) E/AndroidRuntime( 345): Caused by: java.lang.ClassCastException: android.widget.TextView E/AndroidRuntime( 345): at com.test.MainActivity.onCreate(MainActivity.java:34) E/AndroidRuntime( 345): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) E/AndroidRuntime( 345): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611) E/AndroidRuntime( 345): ... 11 more W/ActivityManager( 78): Force finishing activity com.test/.MainActivity W/ActivityManager( 78): Activity pause timeout for HistoryRecord{4059b300 com.test/.MainActivity} W/ActivityManager( 78): Activity destroy timeout for HistoryRecord{4059b300 com.test/.MainActivity} I/Process ( 345): Sending signal. PID: 345 SIG: 9 I/ActivityManager( 78): Process com.test (pid 345) has died. W/InputManagerService( 78): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@405cdc18 |
提前致谢。
尝试将按钮放在下面的代码中,您将获得所需的输出。 希望能帮助到你!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | public class MainActivity extends Activity { private OnClickListener mButtonListener = new OnClickListener() { public void onClick(View v) { try { Intent callIntent = new Intent(Intent.ACTION_CALL); callIntent.setData(Uri.parse("tel:123456789")); startActivity(callIntent); } catch (ActivityNotFoundException activityException) { Log.e("Test","Call failed"); } } }; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button button = (Button) findViewById(R.id.button); try { button.setOnClickListener(mButtonListener); } catch (IOException e) { // TODO Auto-generated catch block System.out.println("Your Crash :" + e); e.printStackTrace(); } } }; |
正如@dmon在评论中所说:
Maybe it's just a case of the generated R class being out of sync with the XML (the reference to a TextView seems truly weird), if you're using Eclipse, try cleaning your project.
在命令行上,您可以通过