我正在创建一个简单的Android应用。我遇到问题的部分是当我尝试从类String
获取randomFailureQuotes
值时,重要的方法如下
public String getQuote(String[] quotes, int indexPosition) {
String quoteToBeDisplayed = new String(quotes[indexPosition]);
return quotes[indexPosition];
}
我尝试在我的EditorActivity
类中使用此代码将其设为textView的标签,其代码如下所示。
randomFailureQuotes quote = new randomFailureQuotes();
String Fq = quote.getQuote;
setContentView(R.layout.activity_editor);
TextView quoteDisplay = new TextView(this);
quoteDisplay.setText(Fq);
当我尝试将名为Fq的字符串值设置为方法getQuote
返回的值时,错误就出现了。 IDE说找不到符号getQuote 。
任何帮助将不胜感激。随意在我的代码中指出任何其他无关的错误,因为我是Java和Android开发的新手,并且很乐意学习。
EDIT 根据RScotCarson的要求,这是我的两个类的代码 randomFailureQuotes
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);
}
}
答案 0 :(得分:0)
您创建一个名为quote的randomFailureQuotes类型的新对象。您没有传入该对象的参数,然后您尝试直接访问新创建的对象内的getQuote数据成员。如果你想使用getQuote函数,你至少需要包括括号,所以它看起来像这样:quote.getQuote()
但请记住,你的getQuote函数需要一个在数组中带索引的字符串数组。
getQuote应返回一个字符串,应该没问题。它将返回存储在String数组内的索引值的字符串。你需要确保你已经传入String数组和值,或者在默认构造函数中定义它。
答案 1 :(得分:0)
让我们来看看你的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)
它接受一个字符串数组以及用于索引数组的位置。这意味着当你调用getQuote时,它看起来应该更像......
// 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;
}
}
从你的问题来看,似乎引用存在于另一个类中。你能否从这两个课程中发布剩下的代码?
如果索引和引号数组都属于RandomFailureQuotes类作为成员,那么只要它们已经实例化,就不需要将它们传递给方法。
修改强> 在OP提供了额外的代码之后,问题在randomFailureQuotes类中有一些问题。
OVER() Clause
的一部分。标准Java类看起来像这样...... RandomFailureQuote.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);
}
}
至于EditorActivity,也有问题。我假设你甚至没有使用工具栏,所以我要从activity和activity_layout.xml文件中删除它。
EditorActivity.java
<?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>
然后我修改了xml,只有一个显示单个失败引用的TextView。请注意,必须重新启动应用才能选择不同的报价。
activity_editor.xml
x = "Hello"
x.upcase!
最后,从开发人员那里,我建议您在进入Android之前花些时间学习Java。两者都很复杂,需要一点时间来学习。网上有一百万个步骤教程,可以帮助您开始使用这两个教程。找一个你喜欢的并密切关注它!祝你好运!
答案 2 :(得分:-1)
String Fq = quote.getQuote;
这是指一个名为getQuote的成员变量(字段)。它没有定义。
方法getQuote()是无关的。变量名称和方法名称占用不同的名称空间。