• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Hello World Extended (fails)

Hi
For several days I have been struggling to put a simple program into effect. That program involves a simple button press now when the user presses this button then I wish to change the text of a text field to reflect that change. What ever way I come about this I am bumping my head dealing with errors.

I have the following in line with the suggestions online I have resorted to a second more slimline program.:rolleyes:

Button | Android Developers

WITH

[HIGH]<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:layout_marginTop="30dp"
android:text="My Buttton"
android:eek:nClick="buttonpress"/>

</RelativeLayout>[/HIGH]

And This

[HIGH]public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
TextView display = null;

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

display.setText("my text");

}

public void buttonpress (View view){
TextView display;

display=(TextView) findViewById(android.R.id.textView1);
display.setText("I was here");
}[/HIGH]

This now fails a syntax error on the
display=(TextView) findViewById(android.R.id.textView1);
line two lines from the end. any IDEAS!!!!!!
I admit I am completely stumped here on such a simple problem as an adaptation to a classic "Hello World"
 
display=(TextView) findViewById(android.R.id.textView1);

It should be R.id.textView1

You have also got a problem that display is null and you are trying to set its text.

This version works.

[HIGH]public class MainActivity extends Activity {

TextView display;

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);
display=(TextView) findViewById(R.id.textView1);
display.setText("my text");

}

public void buttonpress (View view){

display.setText("I was here");

}
}[/HIGH]
 
Upvote 0
hi
Thanks for pointing out the error.

I got it working I did have to add another line 11 at line 17 (Your code)
when I tried it without then the program stopped unexpectedly
or similar error.


Should I need an extra line 11?
I did not copy and paste the code just altered mine.

Manythanks again and yes sir I can program :eek: android R honestly
 
Upvote 0
Thought Id stick this here rather than start a new thread.
I have changed my program, should the application Blank Hello world be able to handle the input from a text input field all on its own.

My idea was in the program to have the user type in a string of text and that would be the basses for (after a button press) the hello world text but this crashes the emulator. NO Code has been entered other than XML layout code

As the program runs in the emulator the text field is clicked on (Activating the keyboard) I click on the letter "T" and it crashes.

Surly I don't have to handle onkeypress myself.
 
Upvote 0
gotta setup the edittext in your code.
Have Tried
[HIGH]EditText URLLong;


@Override
protected void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
URLLong = (EditText)findViewById(R.id.editText1);
URLLong.didTouchFocusSelect();
}[/HIGH]
So now it is being referenced but still it fails, I had no idea it was this hard to do simple task's.
I have shown didTouchFocusSelect as simple lines like
[HIGH]URLLong.hint="some hint"[/HIGH]
would not even compile.
 
Upvote 0
I don't understand what you are doing with didTouchFocusSelect()
Well that's the same for me I haven't the foggiest either, it was just something else to throw in their.

but that might not matter for the moment. What happens if you comment line 11 out?

Err no change. you did mean just // the line out.

Thanks for getting back to me, trying to build a url shortener with the help
of the bit.ly api. But if a user cant enter a URL then its a problem.

Basically In the app I click on the Edittext I can enter in one letter off the keyboard then when I enter the second win8 say's the Emulator has stopped working. That's on another comp to this one so no source code sorry. In the end, user would probably long press the text edit and paste url

But for a first app it should have been simple a few buttons and xml parsing to whet the appetite, before moving on to include it as part of a bigger program. I Wish:D
 
Upvote 0
[HIGH]RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="28dp"
android:text="URL to shorten" />

<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:ems="10"
android:hint="URL to shorten" >

<requestFocus />
</EditText>

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText2"
android:layout_below="@+id/editText2"
android:text="Button" />

</RelativeLayout>[/HIGH]

[HIGH]package com.example.reporters;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

EditText URLLong;


@Override
protected void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
URLLong = (EditText)findViewById(R.id.editText2);
//URLLong.didTouchFocusSelect();
}


@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;
}

}
[/HIGH]

Bassicaly the XML file is not complete yet a few more controls have to be added, but its enough to try out the "edit text":p

Thanks again.
 
Upvote 0
Hmm. That works on both the emulator and my phone and I can't see what's stopping it working for you. Maybe try clean and rebuild the project - see if that makes any difference
Thanks for looking, just tried a quick clean and nothing new, a well
a sleep cycle awaits me. GNight:thinking::(


PS.Does not hang on my phone either just an emulator problem.
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones