0

我正在做一本初学者的 android 入门书,代码看起来很简单。但是,它无法运行。当我尝试运行它时,eclipse 崩溃了。

这是我的java代码:

package com.example.gamebeginner;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity implements View.OnClickListener {
    Button button;
    int touchCount;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        button = new Button(this);
        button.setText("Touch me!");
        button.setOnClickListener(this);
        setContentView(button);
    }

    public void onClick(View v){
        touchCount++;
        button.setText("Touched me " + touchCount + " times(s)");
    }
}

活动.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"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world" />

</RelativeLayout>

显现

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.gamebeginner"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="14" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
        android:debuggable="true" >
        <activity
            android:name="com.example.gamebeginner.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>

我在jdk 1.6版上。任何帮助将非常感激。

4

3 回答 3

4

确保您的设备已连接并正常工作。

  1. 转到窗口 -> 显示视图 -> android -> 设备单击确定,以便打开设备列表。确保您在窗口中看到您的设备。
  2. 确保单击设备以突出显示该设备。现在去点击你的 logcat 选项卡。如果您没有打开 logcat 选项卡,请转到窗口 -> 显示视图 -> 其他 -> android -> logcat。确保不要选择已弃用的版本。

还可以尝试杀死并重新启动 adb 守护程序。

adb kill-server然后adb start-server在命令提示符下。如果您adb的路径上没有,则需要引用完整路径adb或在包含adb.

断开并重新连接设备解决了该问题。

于 2012-12-20T21:56:29.710 回答
1

如果您的错误消息(在 logcat 中,对吗?)是java se binary is not responding,我敢说您以某种错误的方式生成代码。您是使用 Eclipse 还是 IntelliJ 来构建您的应用程序?您使用的项目类型是什么 - Java 应用程序或 Android 应用程序?

于 2012-12-20T21:25:30.077 回答
0

首先添加 setContentView 和 afer View

于 2012-12-20T21:15:43.123 回答