java - 初学者 Android 应用程序崩溃

标签 java android

我正在写一本 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)");
    }
}
<小时/>

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"
    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>
<小时/>

list

<?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。任何帮助将不胜感激。

最佳答案

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

  1. 转到窗口 -> 显示 View -> Android -> 设备,然后单击“确定” 打开设备列表。确保您看到您的设备列在 窗口。
  2. 确保单击该设备以使该设备突出显示。 现在点击您的 logcat 选项卡。如果您没有 logcat 打开选项卡,转到窗口 -> 显示 View -> 其他 -> android -> logcat。 确保您没有选择已弃用的版本。

还可以尝试终止并重新启动 adb 守护进程。

adb Kill-server,然后在命令提示符处adb start-server。如果您的路径上没有 adb,则需要引用 adb 的完整路径或在包含 adb 的目录中执行命令>.

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

关于java - 初学者 Android 应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13980553/

相关文章:

java - 在流过滤器之后访问所有元素

java - 在 Spring 中使用 RMI 公开多个对象

java - 如何从注释处理器内部检查包是否存在(在编译时)?

android - osmdroid bonuspack 显示信息窗口

java - Android-安全的客户端服务器通信

java - 执行java命令时如何获取错误消息?

java - 如何在不同线程中处理@KafkaListener方法?

java - 如何使用 Google Web Toolkit 嵌入 Google Wave

java - 从 HashMap 的 ArrayList 中检索数据在 Android 中始终显示 null

android - Ionic Android 键盘问题