繁体   English   中英

尝试制作斐波那契Android应用

[英]Trying to make a Fibonacci Android App

我是一名新手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教程,并进行了一些调整使其可以制作斐波那契。

非常感谢。

编辑:感谢您的快速回复。 我检查了一下,没有activity_display_message.xml,所以我刚刚创建了它并复制了activity_main.xml中的所有内容。 我也将textviewer移到了onCreate方法中。 现在,我正在整理所有内容,因此感谢您使我摆脱了另一个不眠之夜。

setContentView(textView); 在方法之外,我的意思是它是全局的,请确保它与onCreate方法中的方法相同

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM