Android 应用程序启动时获取异常

Android 应用程序启动时获取异常,android,Android,我正在写一个程序,当按下一个按钮时会给某人打电话。然而,每当我启动应用程序时,它都会崩溃,甚至在按下按钮之前。代码如下: package com.test; import android.app.Activity; import android.content.ActivityNotFoundException; import android.os.Bundle; import android.widget.*; import android.view.*; import android.vi

我正在写一个程序,当按下一个按钮时会给某人打电话。然而,每当我启动应用程序时,它都会崩溃,甚至在按下按钮之前。代码如下:

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); } }; 包com.test; 导入android.app.Activity; 导入android.content.ActivityNotFoundException; 导入android.os.Bundle; 导入android.widget.*; 导入android.view.*; 导入android.view.view.OnClickListener; 导入android.content.Intent; 导入android.net.Uri; 导入android.util.Log; 公共类MainActivity扩展了活动{ 私有OnClickListener mButtonListener=新OnClickListener(){ 公共void onClick(视图v){ 试一试{ Intent callIntent=新意图(Intent.ACTION\u调用); setData(Uri.parse(“电话:123456789”); 星触觉; } 捕获(ActivityNotFoundException activityException){ Log.e(“测试”,“调用失败”); } } }; /**在首次创建活动时调用*/ @凌驾 创建时的公共void(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main); Button Button=(Button)findViewById(R.id.Button); setOnClickListener(mButtonListener); } }; 这是我的布局:

<?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启动com.android.internal.os.RuntimeInit


可能只是生成的R类与XML不同步(对TextView的引用似乎很奇怪),如果您使用Eclipse,请尝试清理您的项目


在命令行上,您可以通过
antclean
或。

尝试按下面代码中的按钮来清理项目,您将获得所需的输出。希望有帮助

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

    }

}
};

简单解决方案-

findViewById(R.id.button).setOnClickListener(mButtonListener);

荣誉:)

按钮类型是什么?正如我看到的,崩溃是由于类强制转换异常造成的,您正在将文本视图强制转换为按钮;请检查您使用的是xml而不是文本格式的按钮。

您需要向我们提供确切的异常消息。您可以在LogCat输出中找到它。@Gareth:我假设这只是您的代码示例中的一个输入错误,但活动的右括号后面有一个分号,不应该在那里。我删除了它,它没有任何区别。我实际上已经习惯了C语言编程。另外,我在一个例子中看到了它,并认为它可能会解决一些问题。不用说,它没有。可能只是生成的R类与XML不同步(对TextView的引用看起来真的很奇怪),如果您正在使用Eclipse,请尝试清理您的项目。谢谢!它正在工作。我打电话时又遇到了一个错误,但我会单独解决。
findViewById(R.id.button).setOnClickListener(mButtonListener);