公共类必须在其自己的文件中定义 [英] Public class must be defined in its own file
问题描述
我想使用一个应用程序中两个视图来与用户的行星的重量。重写了Java几次之后,我终于有工作......居多,但对公共类行星它告诉我公众型行星必须在其自己的文件中定义。我走进清单并做活动,但是这并没有什么帮助。行星已经是我的XML文件中的一个名称。如何让公众型到自己的文件?
activity_main.xml中:
<的RelativeLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:工具=http://schemas.android.com/tools
机器人:layout_width =match_parent
机器人:layout_height =match_parent><的TextView
机器人:ID =@ + ID / askwtTextView
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignParentLeft =真
机器人:layout_alignParentTop =真
机器人:layout_marginLeft =17dp
机器人:layout_marginTop =19dp
机器人:文字=@字符串/ askwt/><的EditText
机器人:ID =@ + ID / inputwtEditText
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignLeft =@ + ID / askwtTextView
机器人:layout_below =@ + ID / askwtTextView
机器人:layout_marginTop =26dp
机器人:EMS =10
安卓的inputType =numberDecimal/><按钮
机器人:ID =@ + ID / enterButton
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignLeft =@ + ID / inputwtEditText
机器人:layout_below =@ + ID / inputwtEditText
机器人:layout_marginTop =38dp
安卓的onClick =buttonclick
机器人:文字=@字符串/输入/>< / RelativeLayout的>
Planets.xml:
<?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直><的TextView
机器人:ID =@ + ID / planetTextView
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文字=@字符串/星球/><的TextView
机器人:ID =@ + ID / textViewform2
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文字=/>< RadioGroup中
机器人:ID =@ + ID / radioGroup1
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT> <单选
机器人:ID =@ + ID / mercuryRadio
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
安卓:检查=真
机器人:文字=@字符串/汞/> <单选
机器人:ID =@ + ID / venusRadio
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文字=@字符串/维纳斯/> <单选
机器人:ID =@ + ID / earthRadio
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文字=@字符串/地球/> <单选
机器人:ID =@ + ID / marsRadio
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
安卓:检查=真
机器人:文字=@字符串/火星/> <单选
机器人:ID =@ + ID / jupiterRadio
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文字=@字符串/木星/> <单选
机器人:ID =@ + ID / saturnRadio
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文字=@字符串/土星/> <单选
机器人:ID =@ + ID / uranusRadio
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
安卓:检查=真
机器人:文字=@字符串/天王星/> <单选
机器人:ID =@ + ID / neptuneRadio
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文字=@字符串/海王星/> <单选
机器人:ID =@ + ID / plutoRadio
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文字=@字符串/冥王星/>
< / RadioGroup中>
<按钮
机器人:ID =@ + ID / selectButton
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
安卓的onClick =buttonclick2
机器人:文字=@字符串/选择/><的TextView
机器人:ID =@ + ID / textViewform2
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
/>< / LinearLayout中
JAVA:
包com.deitel.planetaryweight;进口android.os.Bundle;
进口android.app.Activity;
进口android.content.Intent;
导入android.widget *。
进口android.view.View;
进口android.view.Menu;
进口android.widget.Button;
进口android.widget.RadioButton;
进口android.widget.RadioGroup;
进口android.widget.EditText;
进口android.widget.TextView;
进口java.text.DecimalFormat中;公共类MainActivity延伸活动{/ *你应该习惯宣布与正确的知名度一切。比较好的做法是使一切私人和使用公共mutator方法* /
//全局变量
私人双权重;
私人按钮进入; //创建一个按钮//浏览
私人的EditText wtEntry;
私人TextView的txtForm2;@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState); //与第一屏启动
的setContentView(R.layout.activity_main); 进入=(按钮)findViewById(R.id.enterButton); //创建一个editext并指定XML的EditText的资源ID。
wtEntry =(EditText上)findViewById(R.id.inputwtEditText);
txtForm2 =(的TextView)findViewById(R.id.textViewform2);
}
//按钮点击不应该做任何事情,但执行clicky行动。离场初始化,视图创建等的活动。
// buttonclick表单1
公共无效buttonclick(查看视图){
//接收输入从EditText上,将其转换为双(数)。
重量= Double.parseDouble(wtEntry.getText()的toString()); TextView的T2 = NULL;
//屏幕2改变的TextView的值计算值
t2.setText(Double.toString(重量)); //如果你想有一个新的布局,这是最好的开始新的活动。
//它看起来像你想获得信息反馈,所以使用startActivityForResult()。
//的setContentView(R.layout.planets);
意图dataIntent =新意图(这一点,Planets.class);
dataIntent.putExtra(com.deitel.identifier.DATA_WEIGHT,重量);
startActivityForResult(dataIntent,Activity.RESULT_OK);
}
@覆盖
保护无效的onActivityResult(INT申请code,INT结果code,意图数据){
super.onActivityResult(要求code,结果code,数据); //检查结果code是相同的,因为我们有开始活动
如果(结果code == Activity.RESULT_OK){
从意向//获取双倍,使用相同字符串名称(包prefixed)
//或默认值,如果它没有被置1。
双resultWeight = data.getDoubleExtra(com.deitel.identifier.RESULT_WEIGHT,0.0); //现在做一些resultWeight
}
}
}
// PlanetChooser.class
公共类行星延伸活动{
//常量,通常由大写字母标注,声明为static和最后
公共静态最后的双MERCURYFORCE = 0.38;
公共静态最后的双VENUSFORCE = 0.91;
公共静态最后的双EARTHFORCE = 1.00;
公共静态最后的双MARSFORCE = 0.38;
公共静态最后的双JUPITERFORCE = 2.34;
公共静态最后的双SATURNFORCE = 1.06;
公共静态最后的双URANUSFORCE = 0.92;
公共静态最后的双NEPTUNEFORCE = 1.19;
公共静态最后的双PLUTOFORCE = 0.06;私人单选水星,金星,地球,火星,木星,土星,天王星,海王星,冥王星;//无需使用Double对象,而不是原始的,除非你有很好的理由
私人双mercurypf,venuspf,earthpf,marspf,jupiterpf,saturnpf,uranuspf,neptunepf,plutopf,重量;//一个变量就足够了,它似乎。
私人双resultForce;公共无效的onCreate(捆绑S){
super.onCreate(多个);
的setContentView(R.layout.planets); 汞=(单选)findViewById(R.id.mercuryRadio);
金星=(单选)findViewById(R.id.venusRadio);
地球=(单选)findViewById(R.id.earthRadio);
火星=(单选)findViewById(R.id.marsRadio);
木星=(单选)findViewById(R.id.jupiterRadio);
土星=(单选)findViewById(R.id.saturnRadio);
天王星=(单选)findViewById(R.id.uranusRadio);
海王星=(单选)findViewById(R.id.neptuneRadio);
冥王星=(单选)findViewById(R.id.plutoRadio);
}
公共无效buttonclick2(查看视图){
/ *
在我看来这里你希望看到哪些复选框被选中,并设置基于值
该行星。因为实例变量(在此情况下mercurypf,jupiterpf等)被初始化
默认值(0),则无需进行手动设置。
* / // code用来确定单选按钮被选中哪个星球:
如果(mercury.isChecked()){
resultForce = MERCURYFORCE *重;
}
如果(venus.isChecked()){
resultForce = VENUSFORCE *重;
}
如果(earth.isChecked()){
resultForce = EARTHFORCE *重;
}
如果(mars.isChecked()){
resultForce = MARSFORCE *重;
}
如果(jupiter.isChecked()){
resultForce = JUPITERFORCE *重;
}
如果(saturn.isChecked()){
resultForce = SATURNFORCE *重;
}
如果(uranus.isChecked()){
resultForce = URANUSFORCE *重;
}
如果(neptune.isChecked()){
resultForce = NEPTUNEFORCE *重;
}
如果(pluto.isChecked()){
resultForce = PLUTOFORCE *重;
}
//创建一个新的数据意图传递回调用活动,结果设置code,
//手动完成()这一活动。
意图dataIntent =新意图(本,NULL);
dataIntent.getDoubleExtra(com.deitel.identifier.RESULT_DATA,resultForce);
的setResult(Activity.RESULT_OK,dataIntent);
完();
}
}
清单:
<清单的xmlns:机器人=http://schemas.android.com/apk/res/android
包=com.deitel.planetaryweight
安卓版code =1
机器人:=的versionName1.0><用途-SDK
安卓的minSdkVersion =8
机器人:targetSdkVersion =15/><应用
机器人:图标=@绘制/ ic_launcher
机器人:标签=@字符串/ APP_NAME
机器人:主题=@风格/ AppTheme>
<活动
机器人:名字=。MainActivity
机器人:标签=@字符串/ title_activity_main>
&所述;意图滤光器>
<作用机器人:名字=android.intent.action.MAIN/> <类机器人:名字=android.intent.category.LAUNCHER/>
&所述; /意图滤光器>
< /活性GT;
<活动机器人:名字=行星机器人:标签=@字符串/ title_planets>< /活性GT;
< /用途>< /清单>
您的Java文件名为MainActivity.java(它定义了公共类的名称)。从中取出行星类,并把该类成Planets.java文件。这是pretty太多的Java是多么希望它是。
I am trying to use two views in one application to come up with the user's planetary weight. After rewriting the java several times, I finally have it working... mostly, but on the public class Planets it tells me "The public type planets must be defined in its own file." I went into the manifest and made an activity for it, but that didn't help anything. Planets is already the name of one of my xml files. How do I make the public type into its own file?
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" >
<TextView
android:id="@+id/askwtTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="17dp"
android:layout_marginTop="19dp"
android:text="@string/askwt" />
<EditText
android:id="@+id/inputwtEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/askwtTextView"
android:layout_below="@+id/askwtTextView"
android:layout_marginTop="26dp"
android:ems="10"
android:inputType="numberDecimal" />
<Button
android:id="@+id/enterButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/inputwtEditText"
android:layout_below="@+id/inputwtEditText"
android:layout_marginTop="38dp"
android:onClick="buttonclick"
android:text="@string/enter" />
</RelativeLayout>
Planets.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/planetTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/planet" />
<TextView
android:id="@+id/textViewform2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/mercuryRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="@string/mercury" />
<RadioButton
android:id="@+id/venusRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/venus" />
<RadioButton
android:id="@+id/earthRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/earth" />
<RadioButton
android:id="@+id/marsRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="@string/mars" />
<RadioButton
android:id="@+id/jupiterRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/jupiter" />
<RadioButton
android:id="@+id/saturnRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/saturn" />
<RadioButton
android:id="@+id/uranusRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="@string/uranus" />
<RadioButton
android:id="@+id/neptuneRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/neptune" />
<RadioButton
android:id="@+id/plutoRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/pluto" />
</RadioGroup>
<Button
android:id="@+id/selectButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="buttonclick2"
android:text="@string/select" />
<TextView
android:id="@+id/textViewform2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout
JAVA:
package com.deitel.planetaryweight;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.widget.*;
import android.view.View;
import android.view.Menu;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.EditText;
import android.widget.TextView;
import java.text.DecimalFormat;
public class MainActivity extends Activity {
/* You should get used to declaring everything with the correct visibility. Good practice is to make everything private and use public mutator methods */
//Global variable
private double weight;
private Button enter; // creates a button
// Views
private EditText wtEntry;
private TextView txtForm2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Start with first screen
setContentView(R.layout.activity_main);
enter = (Button) findViewById(R.id.enterButton);
//creates an editext and assigns the resource id of the xml edittext.
wtEntry = (EditText)findViewById(R.id.inputwtEditText);
txtForm2 = (TextView)findViewById(R.id.textViewform2);
}
// Button clicks shouldn't do anything but perform clicky actions. Leave field initialization, view creation, etc to the Activity.
//buttonclick for form 1
public void buttonclick(View view){
//Receives the input from the edittext, converts it to a double (number).
weight = Double.parseDouble(wtEntry.getText().toString());
TextView t2 = null;
//change the value of the textview on screen 2 to the calculation value
t2.setText(Double.toString(weight));
// If you want a new layout, it's best to start a new activity.
// It looks like you want to get information back, so use startActivityForResult().
// setContentView(R.layout.planets);
Intent dataIntent = new Intent(this, Planets.class);
dataIntent.putExtra("com.deitel.identifier.DATA_WEIGHT", weight);
startActivityForResult(dataIntent, Activity.RESULT_OK);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Check that the resultCode is the same as we started the activity with
if(resultCode == Activity.RESULT_OK){
// get the double from the Intent, using the same string name (package prefixed)
// or a default value if it didn't get set.
double resultWeight = data.getDoubleExtra("com.deitel.identifier.RESULT_WEIGHT", 0.0);
// Now do something with resultWeight
}
}
}
// PlanetChooser.class
public class Planets extends Activity {
// constants, usually denoted by uppercase and declared static and final
public static final double MERCURYFORCE = 0.38;
public static final double VENUSFORCE = 0.91;
public static final double EARTHFORCE = 1.00;
public static final double MARSFORCE = 0.38;
public static final double JUPITERFORCE = 2.34;
public static final double SATURNFORCE = 1.06;
public static final double URANUSFORCE = 0.92;
public static final double NEPTUNEFORCE = 1.19;
public static final double PLUTOFORCE = 0.06;
private RadioButton mercury, venus, earth, mars, jupiter, saturn, uranus, neptune, pluto;
// No need to use the Double object as opposed to the primitive unless you have good reason
private double mercurypf, venuspf, earthpf, marspf, jupiterpf, saturnpf, uranuspf, neptunepf, plutopf, weight;
// One variable will suffice, it seems.
private double resultForce;
public void onCreate(Bundle s){
super.onCreate(s);
setContentView(R.layout.planets);
mercury = (RadioButton) findViewById(R.id.mercuryRadio);
venus = (RadioButton) findViewById(R.id.venusRadio);
earth = (RadioButton) findViewById(R.id.earthRadio);
mars = (RadioButton) findViewById(R.id.marsRadio);
jupiter = (RadioButton) findViewById(R.id.jupiterRadio);
saturn = (RadioButton) findViewById(R.id.saturnRadio);
uranus = (RadioButton) findViewById(R.id.uranusRadio);
neptune = (RadioButton) findViewById(R.id.neptuneRadio);
pluto = (RadioButton) findViewById(R.id.plutoRadio);
}
public void buttonclick2(View view){
/*
It looks to me here you're looking to see which box is checked, and set a value based on
that planet. Since instance variables (in this case mercurypf, jupiterpf, etc) are initialized
to the default value (0), there's no need to set them manually.
*/
// Code used to determine which planet RadioButton is checked:
if(mercury.isChecked()) {
resultForce = MERCURYFORCE * weight;
}
if(venus.isChecked()){
resultForce = VENUSFORCE * weight;
}
if(earth.isChecked()){
resultForce = EARTHFORCE * weight;
}
if(mars.isChecked()){
resultForce = MARSFORCE * weight;
}
if(jupiter.isChecked()){
resultForce =JUPITERFORCE * weight;
}
if(saturn.isChecked()){
resultForce = SATURNFORCE * weight;
}
if(uranus.isChecked()){
resultForce = URANUSFORCE * weight;
}
if(neptune.isChecked()){
resultForce = NEPTUNEFORCE * weight;
}
if(pluto.isChecked()){
resultForce = PLUTOFORCE * weight;
}
// Create a new data Intent to pass back to the calling activity, set the result code,
// and manually finish() this activity.
Intent dataIntent = new Intent(this, null);
dataIntent.getDoubleExtra("com.deitel.identifier.RESULT_DATA", resultForce);
setResult(Activity.RESULT_OK, dataIntent);
finish();
}
}
Manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.deitel.planetaryweight"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="Planets" android:label="@string/title_planets"></activity>
</application>
</manifest>
Your Java file is named MainActivity.java (the name of the public class it defines). Remove the Planets class from it, and put that class into a Planets.java file. That's pretty much just how Java wants it to be.
这篇关于公共类必须在其自己的文件中定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!