【发布时间】:2023-12-16 08:32:02
【问题描述】:
我正在编写一个在按下按钮时调用某人的程序。但是,每当我启动应用程序时,它甚至在按下按钮之前都会崩溃。这是代码:
包 com.test; 导入android.app.Activity; 导入android.content.ActivityNotFoundException; 导入android.os.Bundle; 导入 android.widget.*; 导入android.view.*; 导入 android.view.View.OnClickListener; 导入android.content.Intent; 导入android.net.Uri; 导入android.util.Log; 公共类 MainActivity 扩展 Activity { 私人 OnClickListener mButtonListener = new OnClickListener() { 公共无效 onClick(查看 v){ 尝试 { 意图 callIntent = new Intent(Intent.ACTION_CALL); callIntent.setData(Uri.parse("tel:123456789")); 开始活动(调用意图); } 捕捉(ActivityNotFoundException 活动异常){ Log.e("测试", "调用失败"); } } }; /** 在第一次创建活动时调用。 */ @覆盖 公共无效 onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); 设置内容视图(R.layout.main); 按钮按钮 = (Button)findViewById(R.id.button); button.setOnClickListener(mButtonListener); } };这是我的布局:
<?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)
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
提前致谢。
【问题讨论】:
-
您需要向我们提供确切的异常消息。您可以在 LogCat 输出中找到它。
-
-
我删除了它,但没有任何影响。我实际上已经习惯了 C 编程。另外,我在示例中的某个地方看到了它,并认为它可能会解决某些问题。不用说,它没有。
-
也许这只是生成的 R 类与 XML 不同步的情况(对 TextView 的引用似乎真的很奇怪),如果您使用的是 Eclipse,请尝试清理您的项目。跨度>
-
谢谢!它现在正在工作。我在尝试调用时遇到了另一个错误,但我会单独解决。
标签: android