gpt4 book ai didi

android - 应用程序启动时出现异常

转载 作者:行者123 更新时间:2023-12-02 14:16:28 24 4
gpt4 key购买 nike

我正在编写一个程序,当按下按钮时该程序会调用某人。但是,每当我启动该应用程序时,它甚至在按下按钮之前就会崩溃。这是代码:

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

And here is my layout:

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

提前致谢。

最佳答案

尝试将按钮放置在下面的代码中,您将获得所需的输出。希望对您有帮助!

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();

}

}
};

关于android - 应用程序启动时出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6089971/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com