logcat如下:
08-23 14:32:32.586: W/KeyCharacterMap(335): No keyboard for id 0
08-23 14:32:32.586: W/KeyCharacterMap(335): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
08-23 14:32:41.207: D/AndroidRuntime(335): Shutting down VM
08-23 14:32:41.207: W/dalvikvm(335): threadid=1: thread exiting with uncaught exception (group=0x40015560)
08-23 14:32:41.227: E/AndroidRuntime(335): FATAL EXCEPTION: main
08-23 14:32:41.227: E/AndroidRuntime(335): java.lang.ClassCastException: android.widget.Button
08-23 14:32:41.227: E/AndroidRuntime(335): at com.example.android_4_2.MainActivity$1.onClick(MainActivity.java:28)
08-23 14:32:41.227: E/AndroidRuntime(335): at android.view.View.performClick(View.java:2485)
08-23 14:32:41.227: E/AndroidRuntime(335): at android.view.View$PerformClick.run(View.java:9080)
08-23 14:32:41.227: E/AndroidRuntime(335): at android.os.Handler.handleCallback(Handler.java:587)
08-23 14:32:41.227: E/AndroidRuntime(335): at android.os.Handler.dispatchMessage(Handler.java:92)
08-23 14:32:41.227: E/AndroidRuntime(335): at android.os.Looper.loop(Looper.java:123)
08-23 14:32:41.227: E/AndroidRuntime(335): at android.app.ActivityThread.main(ActivityThread.java:3683)
08-23 14:32:41.227: E/AndroidRuntime(335): at java.lang.reflect.Method.invokeNative(Native Method)
08-23 14:32:41.227: E/AndroidRuntime(335): at java.lang.reflect.Method.invoke(Method.java:507)
08-23 14:32:41.227: E/AndroidRuntime(335): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-23 14:32:41.227: E/AndroidRuntime(335): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-23 14:32:41.227: E/AndroidRuntime(335): at dalvik.system.NativeStart.main(Native Method)
代碼如下:
import android.net.Uri;
import android.os.Bundle;
import android.R.string;
import android.app.Activity;
import android.content.Intent;
import android.telephony.PhoneNumberUtils;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bCall = (Button)findViewById(R.id.TelBtn);
bCall.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO 自動生成的方法存根
EditText telnum =(EditText)findViewById(R.id.TelBtn);
String telNum = telnum.getText().toString();
if (PhoneNumberUtils.isGlobalPhoneNumber(telNum)) {
Intent i = new Intent(Intent.ACTION_DIAL,Uri.parse(
"tel://"+telNum));
MainActivity.this.startActivity(i);
} else {
Toast.makeText(MainActivity.this, "號碼不正確", 5000).show();
}
}
}
);
}
真的要把我弄瘋了。。這讓怎麼學習啊~!希望大家幫忙,萬分感謝。。
EditText telnum =(EditText)findViewById(R.id.TelBtn);
這句錯誤,R.id.TelBtn這個指向的是個button應該
Button bCall = (Button)findViewById(R.id.TelBtn);
你看這句,你兩個id都是一樣的,你應該EditText指向的就為布局文件的的EditText
你這邊指向了button,所以出現強制轉換異常