Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/198.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 公共类必须在其自己的文件中定义_Android_Class - Fatal编程技术网

Android 公共类必须在其自己的文件中定义

Android 公共类必须在其自己的文件中定义,android,class,Android,Class,我试图在一个应用程序中使用两个视图来计算用户的行星重量。在多次重写java之后,我终于让它工作了。。。大多数情况下,但在公共类行星上,它告诉我“公共类型的行星必须在它自己的文件中定义。”我进入清单并为它做了一个活动,但这没有任何帮助。Planets已经是我的一个xml文件的名称。如何将公共类型设置为其自己的文件 activity_main.xml: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/andro

我试图在一个应用程序中使用两个视图来计算用户的行星重量。在多次重写java之后,我终于让它工作了。。。大多数情况下,但在公共类行星上,它告诉我“公共类型的行星必须在它自己的文件中定义。”我进入清单并为它做了一个活动,但这没有任何帮助。Planets已经是我的一个xml文件的名称。如何将公共类型设置为其自己的文件

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>
舱单:

<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>


您的Java文件名为MainActivity.Java(它定义的公共类的名称)。从中删除Planets类,并将该类放入Planets.java文件中。这正是Java所希望的。

您的Java文件名为MainActivity.Java(它定义的公共类的名称)。从中删除Planets类,并将该类放入Planets.java文件中。这正是Java所希望的。

是否可以只保留一个Java文件?否。Java不允许这样做(在同一个文件中定义多个公共类)。这是语言的固有特性。@John很抱歉提出这个旧的答案,但是如果你将java代码添加到一个新的java文件中,你一定要以某种方式链接到该文件吗?Thanks@pattyd:简短的回答是“是”。为了得到一个有用的答案,您可能需要打开一个新问题,并添加一些关于您所拥有的以及您正在尝试完成的内容的详细信息。是否可以只保留一个java文件?否。java不允许您这样做(在同一个文件中定义多个公共类)。这是语言的固有特性。@John很抱歉提出这个旧的答案,但是如果你将java代码添加到一个新的java文件中,你一定要以某种方式链接到该文件吗?Thanks@pattyd:简短的回答是“是”。为了得到一个有用的答案,你可能需要打开一个新的问题,并添加一些关于你所拥有的和你想要完成的事情的细节。
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 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>