gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 17:27:14 25 4
gpt4 key购买 nike

我正在写一本 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com