简体   繁体   English

错误:公用类型TodoListItemView必须在其自己的文件中定义。 安卓 日食

[英]Error: The public type TodoListItemView must be defined in its own file. Android. Eclipse

Error: The public type TodoListItemView must be defined in its own file. 错误:公用类型TodoListItemView必须在其自己的文件中定义。 (Line: public class TodoListItemView extends TextView { ) (行: public class TodoListItemView extends TextView {

    package com.kisinivan.todolist;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widget.TextView;

public class MainActivity extends Activity {

    public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
          ListView myListView = (ListView)findViewById(R.id.myListView);
          final EditText myEditText = (EditText)findViewById(R.id.myEditText);
          final ArrayList<String> todoItems = new ArrayList<String>();
        final ArrayAdapter<String> aa;
        aa = new ArrayAdapter<String>(this,
                                      android.R.layout.simple_list_item_1,
                                      todoItems);
        myListView.setAdapter(aa);
           myEditText.setOnKeyListener(new OnKeyListener() {
             public boolean onKey(View v, int keyCode, KeyEvent event) {
               if (event.getAction() == KeyEvent.ACTION_DOWN)
                 if (keyCode == KeyEvent.KEYCODE_ENTER){
                   todoItems.add(0, myEditText.getText().toString());
                   aa.notifyDataSetChanged();
                   myEditText.setText("");
                   return true;
                 }
               return false;
        } });
        }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

public class TodoListItemView extends TextView {
      public TodoListItemView (Context context, AttributeSet ats, int ds) {
        super(context, ats, ds);
        init();
    }
      public TodoListItemView (Context context) {
        super(context);
        init();
    }
      public TodoListItemView (Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }
      private void init() {
      }
      @Override
      public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
      }
    }

Eclipse is applying an optional (but very common) restriction. Eclipse正在应用一个可选的 (但很常见)限制。 From the Java Language Specification, section 7.6 : 根据Java语言规范的第7.6节

If and only if packages are stored in a file system (§7.2), the host system may choose to enforce the restriction that it is a compile-time error if a type is not found in a file under a name composed of the type name plus an extension (such as .java or .jav) if either of the following is true: 当且仅当包存储在文件系统中(第7.2节),如果在文件中未找到由类型名组成的名称的类型,则主机系统可以选择强制实施限制,即编译时错误。如果满足以下任一条件,则加上扩展名(例如.java或.jav):

  • The type is referred to by code in other compilation units of the package in which the type is declared. 该类型由声明该类型的包的其他编译单元中的代码引用。

  • The type is declared public (and therefore is potentially accessible from code in other packages). 该类型被声明为public(因此可以从其他包中的代码访问)。

The fix is simple... one of: 修复很简单...其中之一:

  • Make the class non-public 使班级不公开
  • Make it a nested type (eg within MainActivity ) 使其成为嵌套类型(例如在MainActivity
  • Put it in its own file with a name which matches the class's name 将其放在自己的文件中,该文件名应与类名匹配

Unless you're going to nest it, I'd move it into its own file even if you also make it non-public. 除非您要嵌套它,否则即使您也将其设为非公开,也要将其移动到其自己的文件中。

Eclipse doesnt like you making a class unless it has its own file. Eclipse不喜欢您制作一个类,除非它具有自己的文件。 Just make another file called TodoListItemView and put your class in there. 只需创建另一个名为TodoListItemView的文件,然后将您的类放在此处即可。 Alternitavely, if you are only using the class in one activity, you could just make it private instead. 或者,如果仅在一个活动中使用该类,则可以将其设为私有。

Thanks to Jon skeet I now know that you can also do this 多亏了Jon skeet,我现在知道您也可以这样做

If you want a public class, you can still make one by putting it inside of your activity 如果您想参加公开课,则仍然可以通过在活动中加入公开课

Different Classes need to be defined in their own files. 需要在自己的文件中定义不同的类。 Defining two classes in the same file will not work. 在同一文件中定义两个类将不起作用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 公共类型必须在其自己的文件中定义 - the public type must be defined in its own file Java 编译器错误:“公共类型 .. 必须在其自己的文件中定义”? - Java compiler error: “public type .. must be defined in its own file”? java:4:错误:公用类型NumberAdder必须在其自己的文件中定义 - java:4: error: The public type NumberAdder must be defined in its own file 文件将无法运行:“公共类型EchoTest必须在其自己的文件中定义” - File will not run: “The public type EchoTest must be defined in its own file” 禁用强制执行“必须在自己的文件中定义公共类型xyz” - Disable enforcement of “The public type xyz must be defined in its own file” 枚举类型“公共类型EngineType必须在其自己的文件Car.java中定义”的Java编译错误 - Java compilation Error for an enum type “The public type EngineType must be defined in its own file Car.java” 公共类型 PlayerQuitListener 必须在它自己的文件中定义 - The public type PlayerQuitListener must be defined in its own file 公共类型Dog必须在其自己的文件中定义 - The public type Dog must be defined in its own file 公共类型SalutonFrame必须在其自己的文件中定义 - The public type SalutonFrame must be defined in its own file “必须在其自己的文件中定义公共类型”,但文件名和类名相同 - “The public type must be defined in its own file” but the file name and the class name is the same
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM