java - 使用 Eclipse(Android) 奇怪的错误

标签 java android eclipse

我仍在学习编码,但我一直编写的应用程序无法在 2 种不同的设备上运行。 Eclipse(最新的android sdk)可以是错误还是我每个应用程序都写错了? 它在 2 个设备上强制关闭

    public class MainActivity extends Activity {


Button dugme = (Button) findViewById(R.id.dugme);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    dugme.setOnClickListener(new View.OnClickListener(){

@Override
    public void onClick(View v) {
            Cao();
        }   
        });



}
private void Cao(){
    Intent Cao = new Intent(this, Cao.class);
    startActivity(Cao);
}}

这是曹类

    public class Cao  extends Activity{


protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.cao);
}}

和 list

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

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

    <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.book1.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>
       <activity
        android:name="com.example.book1.Cao"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.Action.VIEW" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
      </activity>
      </application></manifest>

最佳答案

尝试放

Button dugme = (Button) findViewById(R.id.dugme);

onCreate 方法中的这一行。一个类应该只有变量和方法。这条线是定义。所以我认为这只是您代码中的问题。除了您的代码没有任何问题。

关于java - 使用 Eclipse(Android) 奇怪的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15902481/

相关文章:

java - 插入新记录时 hibernate 缓存查询未更新

java - 从对象向下转换为整数运行时错误: java. lang.ClassCastException

eclipse - 使用Eclipse在Scala中创建单文件可运行Jar

java - Eclipse 无法构建,但 Maven 可以,因为从 Java 8 迁移到 Java 11

java - 在 JAVA 的静态 Web 服务中处理复杂对象的最佳实践是什么

java - Powershell 无法将 Java 识别为 cmdlet、函数、脚本文件或可操作程序的名称

java - 从 Android 设备获取所有联系人错误

android - 通话时间禁用设置按钮

android - 如何检查 SharedPreferences 文件是否存在

java - 从 Eclipse 插件编辑 web.xml 的 API?