【发布时间】:2014-10-30 09:15:07
【问题描述】:
我是一名新手 android 应用程序开发人员,我想通过制作一个应用程序来挑战自己,该应用程序接受用户输入、转换为整数并输入斐波那契公式。结果应显示所有斐波那契数,将公式循环用户给定的次数。
到目前为止,我认为我已将问题隔离到 ContentViewer,但再次感谢任何帮助。顺便说一句,如果有帮助,我正在使用 Eclipse Juno
MainActivity.java
package com.example.myfirstapp;
//importing the API for this app
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends Activity {
//I based this on the first app I learnt (as I haven't done much app development so far)
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
//this method creates a default layout when a "new app" is started
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
//This method sends the information of the user input to a second activity screen,
//through the use of intents
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
// Do something in response to button
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
输入进入名为 DisplayMessageActivity.java 的第二个活动屏幕
package com.example.myfirstapp;
//import all the relevant API for this app
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.TextView;
//main class extending Activity superclass
public class DisplayMessageActivity extends Activity {
//This main method is where the fibonacci formula is worked out the result is shown
//For some reason, the ContentViewer is not working for me.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
int messages = Integer.parseInt(message);
int[] fib = new int[messages + 1];
fib[0] = 1;
fib[1] = 1;
for (int i=2; i <= messages; i++)
{
fib[i] = fib[i-1] + fib[i-2];
}
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(60);
StringBuilder builder = new StringBuilder();
for (int j : fib)
{
builder.append(j);
}
String text = builder.toString();
textView.setText(text);
}
// Set the text view as the activity layout
setContentView(textView);
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
我得到的错误信息是
DisplayMessageActivity.java
activity_display_message cannot be resolved or is not a field (Line 18)
Return type for this method is missing (Line 46)
Syntax error on token "textView", VariableDeclaratorId expected after this token (Line 46)
textView cannot be resolved to a type (Line 46)
希望这是足够的信息。有趣的是,我按照 developer.android.com 教程制作了第一个应用,并进行了一些调整以使其符合斐波那契。
非常感谢。
编辑:感谢您的快速回复。我检查并没有activity_display_message.xml,所以我只是创建了它并从activity_main.xml 中复制了所有内容。我还将 textviewer 移动到 onCreate 方法内部。现在我正在整理一切,谢谢你让我从另一个不眠之夜中解脱出来。
【问题讨论】:
-
activity_display_message 这个 xml 在布局文件夹中..log 说它不存在
-
您确定您的文件
activity_display_message.xml已就位吗? -
尝试使用 addContentView(textView) 而不是 setContentView(textView)。
-
向我们展示您的“activity_display_message”布局文件
-
我没有相应的布局文件。我现在做。
标签: java android eclipse android-activity fibonacci