Can someone look over my curly braces? - java

I don't understand the concept with curly braces. It's getting annoying. I have 4 open and 4 closing curly braces. Shouldn't this negate any errors with them then?
package net.androidbootcamp.starconstellations;
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.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b=(Button)findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(MainActivity.this, Pegasuss.class));
}
}
}
}

If you're using eclipse, press Ctrl+Shift+F to auto-indent, and then your code will be very easy to read and you'll be able to follow the blocks created by the curly braces.
It's always a good idea to keep the code correctly indented.

Your on click listener doesn't have a closing normal bracket ')':
b.setOnClickListener(new OnClickListener(){
....
});

public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b=(Button)findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(MainActivity.this, Pegasuss.class));
}
});
}
}
This code should do the trick. The problem is you need to close the parenthesis and the line b.setOnClickListener ( Your on click listener );. I would also recommend formatting your code better in the future as it will make it easier to deal with parenthesis and bracket issues.

Related

Invalid Method Declaration - Return type required

Having issues with the line I labelled. Tried multiple different suggestions but none seem to fix the issue, anyone has anymore suggestions?
package connect2you.com;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
findViewById(R.id.textViewSignup).setOnClickListener(this); // error on this line
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.textViewSignup:
startActivity(new Intent(this, SignUpActivity.class));
break;
}
}
}
Put your call tobfindViewById() inside a method. You can't run code outside of methods.

event can't run in android ( get errors)

I recently use android studio instead of eclipse.
but somthing is wrong!
these are main java code that run currectly in eclipse but now get errors!
package com.example.amir.myapplicationeeee;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
Button button=(Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
startActivity(new Intent(getApplicationContext(),Main2Activity.class));
finish();
}
});
setOnClickListener is hilighed red and error ";" excepted showed when want to runnin app in virtual device
If the code you provide here is exactly the same as you have in your project, then you should just add '}' on the end of your code - it closes the MainActivity class.
You should also have evertyhing implemented inside onCreate() method.
So it should look like:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button=(Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
startActivity(new
Intent(this,Main2Activity.class));
finish();
}
});
}
}

setOnClickListener Error: No Idea Why?

I am new to Android Development, and I can't for the life of me, figure out why this is giving me an error. The error is occurring on this line: 'bu.setOnClickListener(new OnClickListener() {'
I am following the Android Development Fundamental tutorials on Lynda.com; and my code is identical to that of the teachers; or so I think.
package com.lynda.lyndaproject;
import android.os.Bundle;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class Main extends Activity {
Button bu = (Button) findViewById(R.id.button1);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bu.setOnClickListener(new OnClickListener() {
public void onClick(View v){
}
});
}
}
By the way, Eclipse doesn't seem to be allowing me to import anything; and I do believe importing 'android.view.View' should have imported everything necessary.
First, you need to fix your import. You are using the wrong OnClickListener. Change the line
import android.content.DialogInterface.OnClickListener;
to
import android.view.View.OnClickListener;
Next, You need to move the line
Button bu = (Button) findViewById(R.id.button1);
To after the call to setContentView(...). So your onCreate method would look like:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bu = (Button) findViewById(R.id.button1);
bu.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v){
}
});
}
This is because findViewById(...) looks for a View in the current layout. Since this has not been set by setContentView, it will always return null.

Set method return type to 'void'. Dont quite understand

Doing a basic android background service app.
Do not quite understand why there is a error (MainActivity.java). Error is at btnStart = (Button)findViewById(R.id.btnStart);
The quick fix provided was set return type to 'void'. Whereas for btnStop there is no error.
package com.example.backgroundservice;
import android.os.Bundle;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.app.Service;
public class MainActivity extends Activity implements OnClickListener{
btnStart = (Button)findViewById(R.id.btnStart); (ERROR HERE)
btnStop = (Button) findViewById(R.id.btnStop);
btnStart.setonClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent serviceIntent = new Intent(MainActivity.this,MyService.class);
startService(serviceIntent);
}
});
btnStop.setonClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent serviceIntent = new Intent (MainActivity.this,MyService.class);
stopService(serviceIntent);
}
});
}
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}
Your code should be inside the onCreate method.
Before that method is called, your activity is not initialised, so there's no layout, and you can't find any UI element by id.
EDIT: Something like that would work:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout); // this layout must contain btnStart and btnStop
Button btnStart = (Button) findViewById(R.id.btnStart); // variables are declared then allocated
Button btnStop = (Button) findViewById(R.id.btnStop)
// ...<rest of your code>....
It should be inside onCreate.
Button btnStart,btnStop; // should be delcared. i guess you do not have this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.yourlayout);
btnStart = (Button)findViewById(R.id.btnStart); // initialize here
btnStop = (Button) findViewById(R.id.btnStop)
...// rest of the code
I guess you have the below outside onCreate (outside any method) and you have not declared btnStart
btnStart = (Button)findViewById(R.id.btnStart);
But even if you declare you need to inflate the layout first ans then initialize button or else you get NUllPointerException.
So Declare the buttons as class member and initialize it in onCreate as shown above
Edit:
Since you already have the listener annonymous inner class there is no need for you to implement OnClickListener and so you can remove this
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
Also your import staments are wrong
Replace this
import android.content.DialogInterface.OnClickListener;
BY
import android.view.View.OnClickListener;
You must override the activity onCreate method and set their content:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.theLayoutThatContains_R.id.btnStart);
}
You get the error because you have code belonging to method body inside a class body. Eclipse notices the syntax error and proposes to "fix" it by changing the code to a method declaration. You don't get any further syntax errors as compilation stopped at the first syntax error.
As others have instructed, the correct place for code like this is the activity's onCreate() method.

setOnClickListener in simple Android does not work

I try to learn JAVA and I try to write an app for Android. My Code is simple and often I've seen code like this. But when I push the second time a button, the message does not return. The first time it works. What is my error?
package com.test.helloworld;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class HelloWorldApp extends Activity {
private Button closeButton;
private Button buttonAnswer1;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
buttonAnswer1 = (Button)findViewById(R.id.button1);
closeButton = (Button)findViewById(R.id.buttonEnde);
buttonAnswer1.setFocusable(false);
closeButton.setFocusable(false);
buttonAnswer1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setContentView(R.layout.main);
showToastMessage("1");
}
});
closeButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setContentView(R.layout.main);
showToastMessage("2");
}
});
}
private void showToastMessage(String msg){
Toast toast = Toast.makeText(this, msg, Toast.LENGTH_SHORT);
toast.show();
}
}
Don't call the setContentView method inside the click listener:
buttonAnswer1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showToastMessage("1");
}
});
In your onClick functions, you are replacing the entire content view, which will replace the existing button objects with new instances. These new instances no longer have any OnClickListeners.
There is no reason to replace the content view in this case, so the solution is to eliminate those calls from the onClick functions. But if for some reason you needed to replace the content view, then you would need to go through the entire process of finding the new buttons and calling setOnClickListener for each.

Categories