Getting an exception when app starts
I am writing a program that calls someone when a button is pressed. However, whenever I start the app, it crashes, before even pressing the button. This is the code:
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>
Here is the error I'm getting (from 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
Thanks in advance.
Try to put your button like in the code below and you would get your desired output. Hope it helps!
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();
}
}
};
As @dmon said in the comments:
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.
On the command line, you can clean the project via ant clean
or ./gradlew clean
.
上一篇: 如何通过我的Android应用程序在Facebook上分享照片?
下一篇: 在应用程序启动时获取例外