Android Layout fails to open - java

I have this code in one of the java file of my application.
public class Board_Play1 extends Activity {
int d,a=0,b=0,turn=2;
Random random = new Random();
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.board_play1);
while(a!=100 && b!=100)
{
if(turn%2==0)
{
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
// TODO Auto-generated method stub
d=random.nextInt(6)+1;
EditText diceno = (EditText) findViewById(R.id.editText1);
diceno.setText(String.valueOf(d));
}
});
}turn++;
}
}
}
I come to this java file from another java file. All the problem I get is when this file doesn't have any while loop as in code it runs fine. But with including the while loop on navigating to this layout turns screen black and no further process can be done. If we press back button we have a pop out message saying Your application isn't responding. Do You want to close? Yes No.
Why is that happening. All things I included in while loop are perfect. What is causing for this problem?

I think you have an infinite loop. the condition in your while loop is always true because a and b values are never incremented.
And the reason why you're seeing black screen is that this infinite loop is blocking the Main UI Thread.

Seems that this is an infinite loop to me.
int a=0, b=0;
These values never change in your code and you are using them in your while loop's conditional.
Try something like:
while(a!=100 && b!=100)
{
if(turn%2==0)
{
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
d=random.nextInt(6)+1;
EditText diceno = (EditText) findViewById(R.id.editText1);
diceno.setText(String.valueOf(d));
}
});
}
turn++;
// a = somevalue based on what you're trying to do.
// b = somevalue based on what you're trying to do.
}

Related

Double Click on Button Android

First off this question has been asked multiple times, however, none of these questions have been answered to any extent. I have one example that works in the main activity class:
final Button button = (Button) findViewById(R.id.viewcatalog);
button.setFocusable(true);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.find_item);
}
});
But all of my other attempts to replicate this in sequential pages has resulted in failure. I know the reason that they won't work the same way is that my buttons are instantiated in other classes and not in the host class. What is the correct way to fix this error?
The method that doesn't work for reference:
public void OnClickSearch(View view) {
final Button button = (Button) findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
EditText text = (EditText)findViewById(R.id.editText);
String value = text.getText().toString();
setContentView(R.layout.search_results);
}
});
}
It sounds like you are mis-understanding how the UI works in Android.
It is not normally expected that you will change an Activity's view on the fly as your are doing in your OnClickListener.
Instead, you should do one of two things. Either switch to a new Activity, using an Intent and the Activity's startActivity method, or use Fragments, and replace a Fragment in your Activity with a new Fragment.

how to use java onclick shuffle array

I am learning how to use strings and onlclick in java. I have written a programme below which shuffle three names and then outputs them into three buttons.
When I click on Paul, I want the message to be displayed in message box. Since Paul will be in a button each time. I am puzzled on how to attach my message to Paul.
Paul moves around due to the use of array. I understand this is a tough question, but I also know, there are some very clever ppl out there who love a challenge.
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void generate(View view) {
String [] names = new String[3];
names[0] = "Bob";
names[1] = "Paul";
names[2] = "Mike";
Button btn_a = (Button) findViewById(R.id.a);
Button btn_b = (Button) findViewById(R.id.b);
Button btn_c = (Button) findViewById(R.id.c);
TextView message = (TextView)findViewById(R.id.message);
Arrays.asList(names);
Collections.shuffle(Arrays.asList(names));
btn_a.setText(names[0]);
btn_b.setText(names[1]);
btn_c.setText(names[2]);
}
public void a1(View view) {
}
public void b1(View view) {
}
public void c1(View view) {
}
}
This is a trick practical implementation in Java where a single listener is used for multiple buttons, rather than one listener for each button, so that each button's content determines what happens, not each button's listener. Helps for dynamic button grids (i.e. an 8x8 chessboard) to not define 64 listeners and code them all.
I don't have an Android IDE on hand, so this is pseudo-code, but you should be able to get the gist from this.
//Create a Universal Listener for all our buttons
OnClickListener listener = new View.OnClickListener() {
public void onClick(View v) {
Button b = (Button)v;
String text = b.getText().toString(); //get the button's name
if(text.equals("Paul")) {
//do anything for Paul ONLY in here
}
}
});
btn_a.setOnClickListener(listener); //give all the buttons the same listener, but only Paul's listener will do anything when you click on it
btn_b.setOnClickListener(listener);
btn_c.setOnClickListener(listener);
Using info from: http://developer.android.com/reference/android/widget/Button.html and https://stackoverflow.com/a/5620816/2958086

Get text of dynamic generated TextView when clicking on it

I'm running through a for-loop and am generating TextViews that should be clickable because I want to start then an intent and pass the url as parameter as well as the source.
So, I've tried this
articleURL[i].setPaintFlags(articleURL[i].getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
articleURL[i].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//System.out.println(articleURL[v.getId()].getText().toString());
System.out.println(v.getId());
}
});
The problem i encounter is that the v.getId() is always 0. And when i use the commented code
System.out.println(articleURL[v.getId()].getText().toString());
I get an exception that says
java.lang.ArrayIndexOutOfBoundsException: length=10; index=-1
I just need the content of the TextView i clicked on. How exactly do i get it? articleURL[i] doesn't work because he doesn't know i then. How can v.getId() always be -1? No matter which one I click?
This here is the complete for-loop
TextView articleURL = new TextView[hashMapSize];
for (int i = 0; i < hashMapSize; i++) {
articleURL[i] = new TextView(getActivity());
articleURL[i].setPaintFlags(articleURL[i].getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
articleURL[i].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
System.out.println(articleURL[v.getId()].getText().toString());
//System.out.println(v.getId());
}
});
}
You actually get the View in the parameter. Just cast it to TextView and call getText()
public void onClick(View v) {
Log.d("text",((TextView) v).getText().toString());
}
Also don't use System.out.println. This is Android, not desktop Java, and coding android is a huge difference to normal Java. You should get a book on Android and read it, otherwise your apps will start crashing pretty soon and you won't have any chance to fix them.
You may try the following:
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
System.out.println(((TextView)v).getText());
}
};
// ... some loop
articleURL[i].setOnClickListener(listener);
If you also want to get index of item, try this:
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
System.out.println(v.getTag());
}
};
// ... some loop
articleURL[i].setTag(i);
articleURL[i].setOnClickListener(listener);
Try this..
use this as globel
TextView articleURL[];
and then initial the articleURL like below
articleURL = new TextView[hashMapSize];
and then if your extends fragement means use below
articleURL[i] = new TextView(getActivity());
extends activity means
articleURL[i] = new TextView(this);
and
System.out.println(((TextView)v).getText().toString());

What's wrong with my OnClickListener?

I have this jokes app on Google Play (Punny Jokes) and to get a joke, the user must tap anywhere on the screen, then on the joke screen, they get to read the joke. But when they want another joke, they must go back to the main screen and press the screen again, which tends be annoying. I'm trying to set up another full-screen button on the joke screen activity, so they don't have to go back. The jokes are in strings in I have code that selects a random random string in a class called "StartingPoint". Thanks so much!
public class DisplayMessageActivity extends Activity implements OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
**//ERROR BEGINS HERE**
Button next;
Button next = (Button) = findViewById (R.id.next);
next.setOnClickListener(this);
**//ERROR ENDS HERE**
initTypeface1();
}
#Override
public void onClick(final View v) {
switch(v.getId()){
case R.id.next:
IntentHandler.switchActivity(DisplayMessageActivity.this,
StartingPoint.class, false);
break;
// TODO Auto-generated method stub
}
}
};
You have declared the field next twice. And you had an equal sign in a completely wrong place.
Button next = (Button) findViewById (R.id.next);
next.setOnClickListener(this);
or
Button next;
next = (Button) findViewById (R.id.next);
next.setOnClickListener(this);
and solution is also in this block
//ERROR BEGINS HERE
Button next;
next = (Button) findViewById(R.id.next);
next.setOnClickListener(this);
//ERROR ENDS HERE
do replace your code as above
Well you should modify code like this
Button next = (Button) findViewById (R.id.next);
and why do you use next value twice??

Hello World Multiple Buttons Separate Actions

I'm new to android development, and haven't programmed GUI's in java yet so button work is all new to me.
I'm making a simple hello world app, has some buttons/radios/checkboxes etc. We have to figure out a way to make it nice, there is nothing specific in the brief. so I figured I'd get some buttons and show the different kinds of toast, maybe change the background etc.
so I implemented a toast based off a tutorial, but it works on all of the instantiated buttons instead of just the one I want. I would like the other button to do something else.
I think it has to do with the onClickListener, but beyond that I'm stuck.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn1 = (Button) findViewById(R.id.button1);
btn1.setOnClickListener(this);
btn2 = (Button) findViewById(R.id.button2);
btn2.setOnClickListener(this);
cb=(CheckBox)findViewById(R.id.check);
cb.setOnCheckedChangeListener(this);
browser=(WebView)findViewById(R.id.webkit);
browser.loadUrl("http://www.google.com/search");
}
public void onClick(View v) {
new AlertDialog.Builder(this).setTitle("MessageDemo").setMessage(
"This is an Alert Dialogue Toast").setNeutralButton(
"Here, here!", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dlg, int sumthin) {
Toast.makeText(HelloWorldActivity.this,
"<clink, clink>", Toast.LENGTH_SHORT).show();
}
}).show();
}
and here's the xml for the buttons
<Button android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Testa"
android:layout_weight="0.2"></Button>
You've added the same onClickListener to each button so they will have the same behavior. You can actually create the listener right inside the setOnClickListener call, like this:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn1 = (Button) findViewById(R.id.button1);
btn1.setOnClickListener( new OnClickListener() {
public void onClick(View v) {
new AlertDialog.Builder(this).setTitle("MessageDemo").setMessage(
"This is an Alert Dialogue Toast").setNeutralButton(
"Here, here!", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dlg, int sumthin) {
Toast.makeText(HelloWorldActivity.this,
"<clink, clink>", Toast.LENGTH_SHORT).show();
}
}).show();
}
});
btn2 = (Button) findViewById(R.id.button2);
btn2.setOnClickListener( new OnClickListener() {
public void onClick(View v) {
// Do something different here.
}
});
// The rest of onCreate
}
EDIT: I've updated the answer to make it clear which parts of your original code would go where, but I usually wouldn't stick a big chunk of code like that inside of the onClick as it's not very readable. I'd prefer something more like this:
public void onCreate(Bundle savedInstanceState) {
// other onCreate code
btn1 = (Button) findViewById(R.id.button1);
btn1.setOnClickListener( new OnClickListener() {
public void onClick(View v) {
showBtn1ClickedDialog();
}
});
// other onCreate code
}
private void showBtn1ClickedDialog() {
new AlertDialog.Builder(this).setTitle("MessageDemo").setMessage(
"This is an Alert Dialogue Toast").setNeutralButton(
"Here, here!", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dlg, int sumthin) {
Toast.makeText(HelloWorldActivity.this,"<clink, clink>", Toast.LENGTH_SHORT).show();
}
}).show();
}
In addition to goto10's solution, if you're not interested in defining the listeners in-line, your existing click handler can check the ID of the view:
public void onClick(View view) {
switch (view.getId()) {
case R.id.about_button:
// handle about
break;
// etc.
I tend towards goto10's solution, or even inner classes, rather than a switch statement like this, but it's another option. That said, I'm voting up his/her answer, and not mine.
One reason to use a switch instead of inner classes is memory usage, although with modern devices, this might not be a huge issue--but each inner class does take more space, and if the handler is small, IMO is more efficient to do it this way.
Clicking on any button generates an event which is caught by the onClick eventListener. But it doesn't automatically distinguish between the events as to which click generated the event. The information is contained in the View v and by using a switch case on the view, we can have separate events for different clicks.
#Override
public void onClick(View v) {
switch(v.getId()){
case R.id.btn1:
//Do something here
break;
case R.id.btn2:
//Do something else here
break;
case R.id.btn3:
break;
}
}
A summary of how you can use Listeners in your application.

Categories

Resources