I am a newbie android app developer, and I wanted to challenge myself by making an app where a user input is taken, converted into integer, and entered into the Fibonacci formula. The result should show all the fibonacci numbers, looping the formula a user-given number of times.
So far, I think I have isolated the problem to the ContentViewer, but then again any help would be greatly appreciated. BTW I'm using Eclipse Juno if that helps
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);
}
}
The input goes into a second activity screen called 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);
}
}
The error message I'm getting is
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)
Hope that's enough information. Funnily enough, I was following the developer.android.com tutorial on making the first app, with tweaks to make it do Fibonacci.
Many thanks.
Edit: Thanks for your quick response. I checked and there was no activity_display_message.xml so I just created that and copied everything from the activity_main.xml. I also moved the textviewer to inside the onCreate method. Now I'm just tidying up everything so thanks for relieving me from another sleepless night.
the setContentView(textView);
is out side the method i mean it is a global make sure it is with in onCreate method
you have apparently not created activity_display_message (or another less likely possibility is you are referring to a file that is not in the layouts folder). You are setting the view to a file that does not exist. Also from the code, I can see that you are putting in setContentView() in some random place. It should be within the body of a method (preferably within onCreate() in your case).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.