从另一个 class 获取数据时出错
error when getting data from another class
我正在创建一个简单的 Android 应用程序。我遇到麻烦的部分是当我试图从 class randomFailureQuotes
中获取 String
值时,重要的方法如下
public String getQuote(String[] quotes, int indexPosition) {
String quoteToBeDisplayed = new String(quotes[indexPosition]);
return quotes[indexPosition];
}
我尝试在我的 EditorActivity
class 中使用此代码将其设为 textView 的标签,代码如下。
randomFailureQuotes quote = new randomFailureQuotes();
String Fq = quote.getQuote;
setContentView(R.layout.activity_editor);
TextView quoteDisplay = new TextView(this);
quoteDisplay.setText(Fq);
当我尝试将名为 Fq 的字符串值设置为方法 getQuote
returns 的值时出现错误。 IDE 说 它找不到符号 getQuote。
任何帮助将不胜感激。请随时指出我的代码中任何其他不相关的错误,因为我是 Java 和 Android 开发的新手并且很乐意学习。
编辑
根据 RScotCarson 的要求,这是我来自 classes 的其余代码
随机失败报价
public class randomFailureQuotes {
List<String> quotes = new ArrayList<>();
double indexPosition = (Math.random() * 4);
protected void onCreate(Bundle savedInstanceState) {
quotes.add("Failure Does't mean the game is over, it means try again with Experience");
quotes.add("Failure is an event not a person. Yesterday ended Last night - Zig Ziglar");
quotes.add("We learn from failure not success");
quotes.add("If you're not prepared to be wrong, you'll never come up with anything original - Ken Robinson");
}
public String getQuote(String[] quotes, int indexPosition) {
String quoteToBeDisplayed = new String(quotes[indexPosition]);
return quotes[indexPosition];
}
}
EditorActivity
package com.example.slick.thegiftoffailure;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;
import java.util.Random;
import static android.R.attr.id;
public class EditorActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//the toolbar
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// the quote label
randomFailureQuotes quote = new randomFailureQuotes();
String Fq = quote.getQuote();
setContentView(R.layout.activity_editor);
TextView quoteDisplay = new TextView(this);
quoteDisplay.setText(Fq);
}
}
字符串 Fq = quote.getQuote;
这里引用了一个名为getQuote的成员变量(字段)。它在任何地方都没有定义。
getQuote() 方法不相关。变量名和方法名占用不同的命名空间。
您创建了一个名为 quote 的 randomFailureQuotes 类型的新对象。您没有为该对象传递参数,然后您尝试直接访问新创建的对象内的 getQuote 数据成员。如果您想使用 getQuote 函数,您至少需要包含括号,因此它看起来像这样:quote.getQuote()
但请记住,您的 getQuote 函数需要一个字符串数组,数组中有一个索引。
getQuote 应该 return 一个字符串,应该没问题。它将 return 存储在 String 数组内部索引值处的字符串。但是你需要确保你已经传入了字符串数组和值,或者在默认构造函数中定义了它。
让我们看看您的 getQuote 定义:
public String getQuote(String[] quotes, int indexPosition)
它接受一个字符串数组以及一个用于索引数组的位置。这意味着当您调用 getQuote 时,它应该看起来更像这样...
// quotes is an array declared and instantiated previously
// index is an integer that is also declared and instantiated previously
String Fq = quote.getQuote(quotes, index)
从你的问题来看,引用似乎存在于另一个 class 中。你能 post 两个 classes 的其余代码吗?
如果索引和引号数组都属于 RandomFailureQuotes class 作为成员,那么只要它们已经被实例化,就不需要将它们传递给方法。
编辑
在 OP 提供了额外的代码之后,问题出在 randomFailureQuotes class 有几个问题。
- onCreate() 不是构造函数。 onCreate 是 Android 框架的一部分,也是 Activity lifecycle 的一部分。标准的 Java class 看起来像这样...
RandomFailureQuote.java
// It is Java convention to have class names capitalized each word
public class RandomFailureQuotes {
private List<String> quotes = new ArrayList<>();
// The constructor does not need any arguments since you are
// generating the quotes yourself.
public RandomFailureQuotes() {
quotes.add("Failure Does't mean the game is over, it means try again with Experience");
quotes.add("Failure is an event not a person. Yesterday ended Last night - Zig Ziglar");
quotes.add("We learn from failure not success");
quotes.add("If you're not prepared to be wrong, you'll never come up with anything original - Ken Robinson");
}
// Because the index and quotes array are member variables,
// you do not need the arguments in the method definition.
// Also, to get a random number each time you get a quote
// you will need to move the random variable to the method.
// you will need to cast the double value of indexPosition to an int value
public String getQuote() {
double indexPosition = (Math.random() * 4);
String quoteToBeDisplayed = quotes.get((int) indexPosition);
return quoteToBeDisplayed;
}
}
EditorActivity也有问题。我假设您甚至没有使用工具栏,所以我将从 activity 和 activity_layout.xml 文件中删除它。
EditorActivity.java
package com.example.slick.thegiftoffailure;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
public class EditorActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_editor);
// the quote label
randomFailureQuotes quote = new randomFailureQuotes();
String Fq = quote.getQuote();
// You must retrieve the textview from your
// activity_layout.xml file
TextView quoteDisplay = (TextView) findViewById(R.id.text_view);
quoteDisplay.setText(Fq);
}
}
然后我将 xml 修改为只有一个 TextView 显示单个失败引号。请注意,必须重新启动该应用才能选择不同的报价。
activity_editor.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/content_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<TextView
android:id="@+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</RelativeLayout>
最后,开发人员建议您在进入 Android 之前花一些时间学习 Java。两者都有复杂性,需要一点时间来学习。网络上有一百万 step-y-step 教程可帮助您开始使用这两种方法。找到你喜欢的并密切关注它!祝学习顺利!
我正在创建一个简单的 Android 应用程序。我遇到麻烦的部分是当我试图从 class randomFailureQuotes
中获取 String
值时,重要的方法如下
public String getQuote(String[] quotes, int indexPosition) {
String quoteToBeDisplayed = new String(quotes[indexPosition]);
return quotes[indexPosition];
}
我尝试在我的 EditorActivity
class 中使用此代码将其设为 textView 的标签,代码如下。
randomFailureQuotes quote = new randomFailureQuotes();
String Fq = quote.getQuote;
setContentView(R.layout.activity_editor);
TextView quoteDisplay = new TextView(this);
quoteDisplay.setText(Fq);
当我尝试将名为 Fq 的字符串值设置为方法 getQuote
returns 的值时出现错误。 IDE 说 它找不到符号 getQuote。
任何帮助将不胜感激。请随时指出我的代码中任何其他不相关的错误,因为我是 Java 和 Android 开发的新手并且很乐意学习。
编辑 根据 RScotCarson 的要求,这是我来自 classes 的其余代码 随机失败报价
public class randomFailureQuotes {
List<String> quotes = new ArrayList<>();
double indexPosition = (Math.random() * 4);
protected void onCreate(Bundle savedInstanceState) {
quotes.add("Failure Does't mean the game is over, it means try again with Experience");
quotes.add("Failure is an event not a person. Yesterday ended Last night - Zig Ziglar");
quotes.add("We learn from failure not success");
quotes.add("If you're not prepared to be wrong, you'll never come up with anything original - Ken Robinson");
}
public String getQuote(String[] quotes, int indexPosition) {
String quoteToBeDisplayed = new String(quotes[indexPosition]);
return quotes[indexPosition];
}
}
EditorActivity
package com.example.slick.thegiftoffailure;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;
import java.util.Random;
import static android.R.attr.id;
public class EditorActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//the toolbar
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// the quote label
randomFailureQuotes quote = new randomFailureQuotes();
String Fq = quote.getQuote();
setContentView(R.layout.activity_editor);
TextView quoteDisplay = new TextView(this);
quoteDisplay.setText(Fq);
}
}
字符串 Fq = quote.getQuote;
这里引用了一个名为getQuote的成员变量(字段)。它在任何地方都没有定义。
getQuote() 方法不相关。变量名和方法名占用不同的命名空间。
您创建了一个名为 quote 的 randomFailureQuotes 类型的新对象。您没有为该对象传递参数,然后您尝试直接访问新创建的对象内的 getQuote 数据成员。如果您想使用 getQuote 函数,您至少需要包含括号,因此它看起来像这样:quote.getQuote()
但请记住,您的 getQuote 函数需要一个字符串数组,数组中有一个索引。
getQuote 应该 return 一个字符串,应该没问题。它将 return 存储在 String 数组内部索引值处的字符串。但是你需要确保你已经传入了字符串数组和值,或者在默认构造函数中定义了它。
让我们看看您的 getQuote 定义:
public String getQuote(String[] quotes, int indexPosition)
它接受一个字符串数组以及一个用于索引数组的位置。这意味着当您调用 getQuote 时,它应该看起来更像这样...
// quotes is an array declared and instantiated previously
// index is an integer that is also declared and instantiated previously
String Fq = quote.getQuote(quotes, index)
从你的问题来看,引用似乎存在于另一个 class 中。你能 post 两个 classes 的其余代码吗?
如果索引和引号数组都属于 RandomFailureQuotes class 作为成员,那么只要它们已经被实例化,就不需要将它们传递给方法。
编辑 在 OP 提供了额外的代码之后,问题出在 randomFailureQuotes class 有几个问题。
- onCreate() 不是构造函数。 onCreate 是 Android 框架的一部分,也是 Activity lifecycle 的一部分。标准的 Java class 看起来像这样...
RandomFailureQuote.java
// It is Java convention to have class names capitalized each word
public class RandomFailureQuotes {
private List<String> quotes = new ArrayList<>();
// The constructor does not need any arguments since you are
// generating the quotes yourself.
public RandomFailureQuotes() {
quotes.add("Failure Does't mean the game is over, it means try again with Experience");
quotes.add("Failure is an event not a person. Yesterday ended Last night - Zig Ziglar");
quotes.add("We learn from failure not success");
quotes.add("If you're not prepared to be wrong, you'll never come up with anything original - Ken Robinson");
}
// Because the index and quotes array are member variables,
// you do not need the arguments in the method definition.
// Also, to get a random number each time you get a quote
// you will need to move the random variable to the method.
// you will need to cast the double value of indexPosition to an int value
public String getQuote() {
double indexPosition = (Math.random() * 4);
String quoteToBeDisplayed = quotes.get((int) indexPosition);
return quoteToBeDisplayed;
}
}
EditorActivity也有问题。我假设您甚至没有使用工具栏,所以我将从 activity 和 activity_layout.xml 文件中删除它。
EditorActivity.java
package com.example.slick.thegiftoffailure;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
public class EditorActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_editor);
// the quote label
randomFailureQuotes quote = new randomFailureQuotes();
String Fq = quote.getQuote();
// You must retrieve the textview from your
// activity_layout.xml file
TextView quoteDisplay = (TextView) findViewById(R.id.text_view);
quoteDisplay.setText(Fq);
}
}
然后我将 xml 修改为只有一个 TextView 显示单个失败引号。请注意,必须重新启动该应用才能选择不同的报价。
activity_editor.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/content_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<TextView
android:id="@+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</RelativeLayout>
最后,开发人员建议您在进入 Android 之前花一些时间学习 Java。两者都有复杂性,需要一点时间来学习。网络上有一百万 step-y-step 教程可帮助您开始使用这两种方法。找到你喜欢的并密切关注它!祝学习顺利!