更新的程序可以完成现在的工作。非常感谢大家!
有这个程序即时写作。它应该显示一个图像按钮,当它推动该程序时应该调用一个特定的电话号码。 Eclipse不会返回任何错误。但是当我在模拟器或我的手机上运行它时,除了不幸的是srshelper(< - prgram名称)已经停止之外它没有任何解释而退出。
这是主要的java-class:
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”位于drawable文件夹中。 HALP! ;)
嘿,谢谢你的快速反应。 logcat报告丢失的文件: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
答案 0 :(得分:0)
我最初的猜测是你错过了在AndroidManifest中拨打电话的权限
答案 1 :(得分:0)
Caused by: java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.Button
ImageButton
不是Button
。您的froschButton
类型在代码和布局之间不一致。