It says Expression expected but no recommendations as to what symbol is missing. I'm using android studio. I suppose it's a simple fix I'm just not sure what symbol.
Heres my code:
public class CloudActivity extends ActionBarActivity implements View.OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cloud);
Button genderButton = (Button) findViewById(R.id.genderButton);
Button button3 = (Button) findViewById(R.id.button3);
genderButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(CloudActivity.this, LoginActivity.class);
startActivityForResult(intent, 0);
}
},
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent tnt = new Intent(CloudActivity.this, LogActivity.class);
startActivityForResult(tnt, 0);
}
}, // Where the problem is
));
}
#Override
public void onClick(View v) {
}
}
You are closing your anonymous inner classes (new View.OnClickListener() {) wrong. You shouldn't be separating them with a comma but closing them off with a );.
For example,
genderButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(CloudActivity.this, LoginActivity.class);
startActivityForResult(intent, 0);
}
});
match up your {} and () for all of them. So you will want to do the same thing for button3.setOnClickListener(new View.OnClickListener() { and any others you might have.
You can read more about them in the Java Docs
Try This. You weren't closing your inner classes properly. I imagine you accidentally put commas in place of semi-colons?
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cloud);
Button genderButton = (Button) findViewById(R.id.genderButton);
Button button3 = (Button) findViewById(R.id.button3);
genderButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(CloudActivity.this, LoginActivity.class);
startActivityForResult(intent, 0);
}
});
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent tnt = new Intent(CloudActivity.this, LogActivity.class);
startActivityForResult(tnt, 0);
}
});
}
Related
I have created a new layout name dialog_exit for onBackPressed but when I install and open, my application can not open and showing error close app
Please review my whole code and guide me how can I solve this problem
Here is my main activity code
public class MainActivity extends AppCompatActivity {
public Dialog mDialog;
public Button mDialogyes, mDialogno;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
createDialog();
}
private void createDialog() {
mDialog = new Dialog(this);
mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
mDialog.setContentView(R.layout.dialog_exit);
mDialog.setCanceledOnTouchOutside(true);
mDialog.setCancelable(true);
mDialogyes = (Button) findViewById(R.id.yes);
mDialogno = (Button) findViewById(R.id.no);
mDialogyes.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setFlags(intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
finish();
System.exit(0);
mDialogno.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mDialog.dismiss();
}
});
}
});
}
#Override
public void onBackPressed() {
mDialog.show();
}
}
Here is my layout code as screenshot because
stackoverflow not allow me to add more code that why sharing image
Updated code of createDialog function
private void createDialog() {
mDialog = new Dialog(this);
mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
mDialog.setContentView(R.layout.dialog_exit);
mDialog.setCanceledOnTouchOutside(true);
mDialog.setCancelable(true);
mDialogyes = (Button) mDialog.findViewById(R.id.yes);
mDialogno = (Button) mDialog.findViewById(R.id.no);
mDialogyes.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setFlags(intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
finish();
System.exit(0);
}
});
mDialogno.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mDialog.dismiss();
}
});
}
try this code
mDialogyes = (Button)mDialogyes. findViewById(R.id.yes);
mDialogno = (Button)mDialogyes. findViewById(R.id.no);
mDialogyes.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setFlags(intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
finish();
System.exit(0);
}
});
mDialogno.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mDialog.dismiss();
}
});
You have to do like this
mDialogyes = (Button) mDialog.findViewById(R.id.yes);
mDialogno = (Button) mDialog.findViewById(R.id.no);
So far I have a button from my second activity that opens a new activity. As shown below..
public class FifthActivity extends Activity {
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fifth_layout);
Button button = (Button) findViewById(R.id.button10);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(FifthActivity.this, AmazonActivity.class);
FifthActivity.this.startActivity(intent);
}
});
}
}
Now the thing is, I have multiple buttons in my fifthActivity.java that I need to make start a new Activity. Going from this code, From what part of the code do I need to put in my FifthActivity.java to make it so the other buttons open?
I don't really know, if I get your question. But I guess your FifthActivity contains some buttons and you want to start different activities with these buttons. (Am I right?)
Heres an short example:
public class FifthActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fifth_layout);
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(FifthActivity.this, Activity1.class);
FifthActivity.this.startActivity(intent);
}
});
Button button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(FifthActivity.this, Activity2.class);
FifthActivity.this.startActivity(intent);
}
});
Button button3 = (Button) findViewById(R.id.button3);
button3.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(FifthActivity.this, Activity3.class);
FifthActivity.this.startActivity(intent);
}
});
}
}
This is really basic android programming. If you struggle with this you should probably do some android tutorials, before starting programming your own app.
I want to pass an XML resource from one activity to another activity using Java Code? I don't want to create separate different activities for different buttons.
ImageButton imageBttn = (ImageButton)findViewById(R.id.imageButton1);
imageBttn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, info.class));
}
});
Pseudocode to explain what I'm trying to do:
If BUTTON_1 is clicked
-Pass swirl.png to info.class
If BUTTON_2 is clicked
-Pass golden.png to info.class
If BUTTON_3 is clicked
-Pass arcade.png
imageBttn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, info.class)
intent.putExtra("image",R.drawable.ic_search_grey);
startActivity(intent);
}
});
On Mainactvitiy get the int drawable
int res = getIntent().getIntExtra("image", -1);
if(res > -1) {
Drawable drawable = getResources().getDrawable(res, null);
imgBtn.setImageDrawable(drawable);
}
InfoActivity.java
public class InfoActivity extends Activity {
private static final String EXTRA_IMAGE = "image";
public static void launch(Activity activity, #DrawableRes int imageResId) {
Intent intent = new Intent(activity, InfoActivity.class);
intent.putExtra(EXTRA_IMAGE, imageResId);
activity.startActivity(intent);
}
#Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_info);
int imageResId = getIntent().getIntExtra(EXTRA_IMAGE, -1);
if (imageResId == -1) {
throw new IllegalArgumentException(EXTRA_IMAGE);
// or set error/default image resource id
}
// ... something to do with imageResId
}
}
MainActivity.java
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
InfoActivity.launch(MainActivity.this, R.drawable.swirl);
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
InfoActivity.launch(MainActivity.this, R.drawable.golden);
}
});
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
InfoActivity.launch(MainActivity.this, R.drawable.arcade);
}
});
}
I'm trying to switch the views, but when I'm in the second view, the back event click doesnt work.. I don't know what's wrong.
Pls, see my code and help me!
Part1
Part2
public class t extends Activity implements OnClickListener {
Button volta;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.janela2);
volta = (Button) findViewById(R.id.button2);
volta.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if (v == volta) {
startActivity(new Intent(t.this, MainActivity.class));
}
}
}
You have to override onBackPressed. Change your MainActivity as below
public class MainActivity extends Activity {
private boolean goBack = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button sobre = (Button) findViewById(R.id.button1);
sobre.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
goBack = true;
setContentView(R.layout.janela2);
}
});
}
#Override
public void onBackPressed() {
//If you have switched to R.layout.janela2 then go back
if (goBack){
setContentView(R.layout.activity_main);
goBack = false;
return;
}
//else do default action
super.onBackPressed();
}
}
Just do the following code, I hope it might help you
MainActivity.java
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button sobre = (Button) findViewById(R.id.button1);
sobre.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, t.class);
startActivity(intent);
}
});
}
}
In t.java
public class t extends Activity{
Button volta;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.janela2);
}
#Override
public void onStop() {
super.onStop();
finish();
}
}
If you want two layouts then use viewflipper. If you want two activities (java classes) AND two layouts separately then use:
Intent i = new Intent (this, myClass.class);
startActivity(i);
To start the Activity and NOT setcontentview
So here:
public void onClick(View v) {
startActivity(new Intent (MainActivity.this, t.class));
OR IN THE CASE OF T.CLASS:
startActivity(new Intent (t.this, MainActivity.class));
}
You have to override onBackPressed() method if you want to back button functionality in your application. i.e.
public void onBackPressed() {
Intent start = new Intent(CurrentClass.this,Next_Activity.class);
startActivity(start);
finishActivity(0);
}
Hello everyone. Today i started writting a code so i could open second activity while i am pressing on the button. And i am keep getting the "{}" brace errors. I also created manifest "android:name=".activity2">" But i can't find the way how should i fix my braces to stop showing error? I uplauded some pictures so you could see a bit better what's the problem. Maybe there is way easier way to open the second activity with the button? Any tips to fix the problem? Thank you :)
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button imeageTextBtn = (Button) findViewById(R.id.imeageTextBtn);
assert imeageTextBtn != null;
imeageTextBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), activity2.class);
startActivityForResult(intent, 0);
}
}
}
You're calling the setOnClickListener method. so it needs a closing ) and a semicolon.
imeageTextBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), activity2.class);
startActivityForResult(intent, 0);
}
});
After imeageTextBtn.setOnclick...., you need to add a ); after the }
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button imeageTextBtn = (Button) findViewById(R.id.imeageTextBtn);
assert imeageTextBtn != null;
imeageTextBtn.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), activity2.class);
startActivityForResult(intent, 0);
}
}
);
}