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.
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 setContentView(textView); is out side the method i mean it is a global make sure it is with in onCreate method
Related
I am joining two activity through intent
package com.smartcodeone.newapp1;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
public static final String STRING_VAR = "com.smartcodeone.newapp1.HELLO_WORLD";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnMsg = (Button) findViewById(R.id.btnMsg);
btnMsg.setOnClickListener(new View.OnClickListener(){
//when user click's this function will be called
public void onClick(View v){
Intent intentvar = new Intent(getApplicationContext(),Main2Activity.class);
intentvar.putExtra(STRING_VAR,"Hello World"); //this is used to pass data to next intent
startActivity(intentvar);
}
});
}
private int findViewId(int btnMsg) {
return 0;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public android.support.v4.app.FragmentManager getSupportFragmentManager() {
return null;
}
}
It might have to do with Android Studio. Try cleaning your project and then rebuilding. If that doesn't work go to File -> Invalidate Caches/Restart ...
I get those issues sometimes as well. Let me know if that works. I tried your code and it works fine.
The issue seems to be that you defined a method named: findViewById(int button) that always return 0.
Use the Activity method instead of your own:
this.findViewById(int resourceId)
Good luck!
It seems you are implementing your own findviewbyid(). I don't know if you intend to do so.
Try removing
private int findviewbyid(int btnMsg) {
}
The ActionBarActivity's findviewbyid should resolve your button from your layout file.
package com.example.desktop.mirror;
import android.content.Intent;
import android.provider.Settings;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.MenuItem;
import android.view.MenuItem;
import android.widget.EditText;
import java.util.Scanner;
public class MainActivity extends ActionBarActivity {
public EditText a;
public String entereda;
public EditText b;
public String enteredb;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void OnClick(View v){
a = (EditText) findViewById(R.id.first_text);
b = (EditText) findViewById(R.id.second_text);
if(boolean entereda){
Intent i = new (this, reverseactivity.class);
startActivity(i);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem menu) {
// 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 = menu.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(menu);
}
I'm new to both Java and Android, so I can not be more specific.
My question might be a silly one, since it's a simple one to be implemented in Java, but coming to implementation on Android, i'm struggling a lot with the code.
Try out this minimal code (2 lines, excluding the Sysyem.out.println()) :
// The big works ;)
StringBuffer buffer = new StringBuffer("Test!");
buffer.reverse();
// Test it out
System.out.println(buffer); // .toString());
Output:
!tseT
Required import:
java.lang.StringBuffer
public void OnClick (View v){
a = (EditText) findViewById(R.id.first_text);
b = (EditText) findViewById(R.id.second_text);
StringBuilder s = new StringBuilder(100);
String entereda = a.getText().toString();
int str_len = entereda.length();
for (int i = (str_len - 1); i >= 0; i--) {
System.out.print(entereda.charAt(i));
s.append(entereda.charAt(i));
}
Not very familiar with android either, but to reverse strings:
public static String reverse(String toReverse) {
Stack<Character> letters = new Stack<Character>();
for(char c:toReverse.toCharArray()) {
letters.push(c);
}
StringBuilder sb = new StringBuilder();
while(!letters.empty()) {
sb.append(letters.pop());
}
return sb.toString();
}
And just have the result show up using a Thread and setting the result to the value of where you want it to go.
Java isn't different from Android, just Android has built-in GUI capabilities.
And how do you get the code to show up in java?
I'm trying to follow this tutorial from Google to create your own android app with Android Studio. But when I follow the 4th step on this page: http://developer.android.com/training/basics/firstapp/starting-activity.html Android Studio ends up with this error:
Cannot resolve symbol 'View'
This is what my code looks like at the moment:
public class MainActivity extends ActionBarActivity {
/** Called when the user clicks the Send button */
public void sendMessage(View view) { <--- (This line ends up with the error)
// Do something in response to button
}
#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.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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
What's wrong with this code? I'm not experienced in java and after looking at other questions I still couldn't find the right solution.
Thanks for helping!
I think you forget to include the import statement for View. Add the following import in your code
import android.view.View;
I am doing the same tutorial and ran into the same problem (that's why I found this question).
I see they explain this issue in the next paragraph named "Build an Intent":
Android Studio will display Cannot resolve symbol errors because this
code references classes that are not imported. You can solve some of
these with Android Studio's "import class" functionality by pressing
Alt + Enter (or Option + Return on Mac). Your imports should end up as
the following:
import android.content.Intent; import
android.support.v7.app.AppCompatActivity; import android.os.Bundle;
import android.view.View; import android.widget.EditText;
https://developer.android.com/training/basics/firstapp/starting-activity.html#BuildIntent
This problem can be resolved easily either by pressing alt + enter on the error to import android.view.View or by cross-checking that your method is outside protected void onCreate(Bundle savedInstanceState) and in the class parenthesis.
I am new to android programming on eclipse and just doing it as a hobby...I have made and run 2 apps successfully and so their is no question of me not knowing how to run the app...i have restarted my pc but the problem sill persists..my 2 apps are still working on pressing the run button and i am using a device to run them(not an AVD) but the third app is not working...in fact their is no response..their are no errors in code but 3 lint warnings..the program is very simple(taking 2 numbers from user and displaying the sum)...Please help.MY code is as following:
package com.example.app3;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.*;
import android.view.View.*;
import android.widget.*;
public class MainActivityapp3 extends ActionBarActivity {
int num1,num2,sum;
Button button1;
EditText textf1,textf2;
TextView textv3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activityapp3);
textf1=(EditText)this.findViewById(R.id.editText1) ;
textf2=(EditText)this.findViewById(R.id.editText2);
textv3=(TextView)this.findViewById(R.id.textView3);
button1=(Button)this.findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
num1 = Integer.parseInt(textf1.getText().toString());
num2 = Integer.parseInt(textf2.getText().toString());
sum = num1 + num2;
textv3.setText("sum" + "");
}
});
}
#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_activityapp3, 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);
}
}
Would really need to see the output of logcat. You can find it in the DDMS tabs of Eclipse.
Anyway, without more information I would ask: did you remember changing the activity name in AndroidManifest?
I'm new to android and have have an assignment to create an email application. So I have 2 layouts and 2 activities, one for reading the email and one for writing the email. I'm trying to retain the information sent in the fields in the email writing activity for when the email is being read. The problem is at the **bolded line below - "The method setText string is undefined for the type view", and I need to get all the text view to contain the information send from the other activity. I can post the other files if its needed, all help appreciated. I have tried other ways to assign the variable to text view but can't seem to get it to work.
DisplayMessageActivity.java
package com.example.project;
import com.example.project.R.layout;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.MenuItem;
import android.widget.TextView;
public class DisplayMessageActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
// Show the Up button in the action bar.
setupActionBar();
// Get the messages from the intent
Intent intent = getIntent();
String messageto2 = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
String messagefrom2 = intent.getStringExtra(MainActivity.EXTRA_MESSAGE2);
String messagecc2 = intent.getStringExtra(MainActivity.EXTRA_MESSAGE3);
String messagebcc2 = intent.getStringExtra(MainActivity.EXTRA_MESSAGE4);
String messagesubject2 = intent.getStringExtra(MainActivity.EXTRA_MESSAGE5);
String messagebody2 = intent.getStringExtra(MainActivity.EXTRA_MESSAGE6);
**TextView msgto = (TextView)findViewById(R.id.to2).setText(messageto2);**
TextView msgfrom = (TextView)findViewById(R.id.from2);
TextView msgcc = (TextView)findViewById(R.id.cc2);
TextView msgbcc = (TextView)findViewById(R.id.bcc2);
TextView msgsubject = (TextView)findViewById(R.id.subject2);
TextView msgbody = (TextView)findViewById(R.id.body2);
}
/**
* Set up the {#link android.app.ActionBar}, if the API is available.
*/
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void setupActionBar() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
getActionBar().setDisplayHomeAsUpEnabled(true);
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
TIA
Try to change this:
(TextView)findViewById(R.id.to2).setText(messageto2);
to
((TextView)findViewById(R.id.to2)).setText(messageto2);
Methods are resolved according to the static type of the reference. findViewById() is declared with a return type of View and since the class View doesn't declare a method setText(), the compiler complains. Use this
TextView msgto = (TextView)findViewById(R.id.to2);
msgto.setText(messageto2);
try this..
((TextView)findViewById(R.id.to2)).setText(messageto2);
TextView msgto = (TextView)findViewById(R.id.to2).setText(messageto2);
Need to be changed to
TextView msgto = (TextView) findViewById(R.id.to2);
msgto.setText(messageto2);
That should help.