更新的程序现在可以执行它应该做的事情。非常感谢大家!
有这个程序正在编写。它应该显示一个图像按钮,当它按下程序时,应该拨打特定的电话号码。Eclipse 不会返回任何错误。但是当我在模拟器或手机上运行它时,它退出没有任何解释,除了不幸的是 srshelper(<- prgram 名称(已停止。
下面是主要的 java 类:
package com.example.srshelper;
import com.example.srshelper.R;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
ImageButton froschButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
froschButton = (ImageButton) findViewById(R.id.ibFrosch);
froschButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:"+"023588778600"));
startActivity(callIntent);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
以下是activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ImageButton
android:id="@+id/ibFrosch"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/frosch"
/>
</RelativeLayout>
图片"Frosch"位于可绘制文件夹中。哗啦啦!;)
嘿,感谢您的快速回复。日志猫报告缺少的文件:
06-18 21:30:29.005: E/Trace(805): error opening trace file: No such file or directory (2)
06-18 21:30:29.105: W/ActivityThread(805): Application com.example.srshelper is waiting for the debugger on port 8100...
06-18 21:30:29.215: I/System.out(805): Sending WAIT chunk
06-18 21:30:29.435: I/dalvikvm(805): Debugger is active
06-18 21:30:29.635: I/System.out(805): Debugger has connected
06-18 21:30:29.635: I/System.out(805): waiting for debugger to settle...
06-18 21:30:29.845: I/System.out(805): waiting for debugger to settle...
06-18 21:30:30.045: I/System.out(805): waiting for debugger to settle...
06-18 21:30:30.255: I/System.out(805): waiting for debugger to settle...
06-18 21:30:30.455: I/System.out(805): waiting for debugger to settle...
06-18 21:30:30.655: I/System.out(805): waiting for debugger to settle...
06-18 21:30:30.855: I/System.out(805): waiting for debugger to settle...
06-18 21:30:31.065: I/System.out(805): waiting for debugger to settle...
06-18 21:30:31.265: I/System.out(805): waiting for debugger to settle...
06-18 21:30:31.465: I/System.out(805): waiting for debugger to settle...
06-18 21:30:31.675: I/System.out(805): debugger has settled (1431)
考虑到许可,只是再次加倍:D但它似乎是设置正确的。以下是清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.srshelper"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.CALL_PHONE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.srshelper.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
好吧,更多的研究让我获得了一个神秘的许可:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
包含该错误后,丢失的文件错误不再显示,而是报告其他一些错误。现在:D要调查它们。
06-19 09:25:31.903: I/Process(864): Sending signal. PID: 864 SIG: 9
06-19 09:35:22.192: D/AndroidRuntime(964): Shutting down VM
06-19 09:35:22.192: W/dalvikvm(964): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
06-19 09:35:22.212: E/AndroidRuntime(964): FATAL EXCEPTION: main
06-19 09:35:22.212: E/AndroidRuntime(964): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.srshelper/com.example.srshelper.MainActivity}: java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.Button
06-19 09:35:22.212: E/AndroidRuntime(964): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
06-19 09:35:22.212: E/AndroidRuntime(964): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
06-19 09:35:22.212: E/AndroidRuntime(964): at android.app.ActivityThread.access$600(ActivityThread.java:141)
06-19 09:35:22.212: E/AndroidRuntime(964): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
06-19 09:35:22.212: E/AndroidRuntime(964): at android.os.Handler.dispatchMessage(Handler.java:99)
06-19 09:35:22.212: E/AndroidRuntime(964): at android.os.Looper.loop(Looper.java:137)
06-19 09:35:22.212: E/AndroidRuntime(964): at android.app.ActivityThread.main(ActivityThread.java:5041)
06-19 09:35:22.212: E/AndroidRuntime(964): at java.lang.reflect.Method.invokeNative(Native Method)
06-19 09:35:22.212: E/AndroidRuntime(964): at java.lang.reflect.Method.invoke(Method.java:511)
06-19 09:35:22.212: E/AndroidRuntime(964): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-19 09:35:22.212: E/AndroidRuntime(964): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-19 09:35:22.212: E/AndroidRuntime(964): at dalvik.system.NativeStart.main(Native Method)
06-19 09:35:22.212: E/AndroidRuntime(964): Caused by: java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.Button
06-19 09:35:22.212: E/AndroidRuntime(964): at com.example.srshelper.MainActivity.onCreate(MainActivity.java:22)
06-19 09:35:22.212: E/AndroidRuntime(964): at android.app.Activity.performCreate(Activity.java:5104)
06-19 09:35:22.212: E/AndroidRuntime(964): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
06-19 09:35:22.212: E/AndroidRuntime(964): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
06-19 09:35:22.212: E/AndroidRuntime(964): ... 11 more
我最初的猜测是你缺少在AndroidManifest中拨打电话的权限
Caused by: java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.Button
ImageButton
不是Button
.代码和布局之间的froschButton
类型不一致。