试图制作斐波纳契Android应用程序

时间:2014-10-30 09:15:08

标签: java android eclipse android-activity fibonacci

我是一个新手Android应用程序开发人员,我想通过创建一个用户输入的应用程序,转换为整数,并输入到Fibonacci公式来挑战自己。结果应显示所有斐波纳契数,将公式循环用户给定的次数。

到目前为止,我认为我已将问题隔离到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教程,并通过调整使其成为Fibonacci。

非常感谢。

编辑:感谢您的快速回复。我检查过,没有activity_display_message.xml所以我只是创建了它并从activity_main.xml中复制了所有内容。我还将textviewer移到了onCreate方法中。现在我只是整理一切,所以感谢你让我从另一个不眠之夜中解脱出来。

2 个答案:

答案 0 :(得分:0)

setContentView(textView);超出方法我的意思是全局确保它与onCreate方法一起

答案 1 :(得分:0)

您显然没有创建activity_display_message(或者另一种可能性较小的可能是您指的是不在layouts文件夹中的文件)。您正在将视图设置为不存在的文件。同样从代码中,我可以看到你在一些随机的地方放入setContentView()。它应该在方法体内(最好在你的情况下在onCreate()内)。