[英]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: 修复很简单...其中之一:
MainActivity
) MainActivity
) 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.