Maybe it sounds silly, but I am getting an error on Button keyword (where it should not be).
I am a newbie, and looked almost everywhere. And everyone says that
Button b = findViewById(R.id.button1);
is correct.
My code:
package com.example.myfirstappnew;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = findViewById(R.id.button1);
}
#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;
}
}
Here is the screenshot:
http://pbrd.co/ZEsSw7
From what I see, you have an error because findViewById returns View and not Button, and you need to cast it to Button.
You need to cast the Button to a Button:
Button b = (Button) findViewById(R.id.button1);
And add this import:
import android.widget.Button;
Next time, when you say you have an error please include it :)
findViewById returns View, you need to cast it into Button.
Button b = (Button) findViewById(R.id.button1);
Edit:
Click on the first link that show import import Button(android.widget.)
or simply press ctrl+shift+o
The code needs to import Button (as suggested in the top hint in the screenshot).
First you need to change
Button b = findViewById(R.id.button1);
to
Button b = (Button) findViewById(R.id.button1);
You also need to add
import java.widget.Button;
to your import statements at the top of your file. If you are using Eclipse, you should use it's "organize imports" feature (or whatever Eclipse calls it) to do this automagically for you.
Related
I'm just started to make my first android app and I'm trying to get more familiar with the basic principles of android developing. So, in no time my MainActivity exploded with lines of code. To make my code more maintainable, i'm trying to put pieces of code in different activities. Also according to the design principles of android: Don't Overload a Single Activity Screen
Now I'm struggling to use different activities with a single XML layout. I found some similar cases here like: this one But i'm also reading here that I should use fragments. I can't see how to work this out properly.
The specific problem I encounter with my code is that the second activity should change the background of the imageview to normal with the setImageResource, but it doesn't.
My code:
package com.test.scores;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
public class MainActivity extends Activity implements View.OnClickListener {
private ImageButton btn1, btn2;
int varMinusScore;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (ImageButton) findViewById(R.id.btn1);
btn1.setOnClickListener(this);
btn2 = (ImageButton) findViewById(R.id.btn2);
btn2.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn1:
varMinusScore = 1;
startActivity(new Intent(getApplicationContext(), ResetImageResources.class));
btn1.setImageResource(R.drawable.btn01p);
}
switch (v.getId()) {
case R.id.btn2:
varMinusScore = 2;
startActivity(new Intent(getApplicationContext(), ResetImageResources.class));
btn2.setImageResource(R.drawable.btn02p);
}
}
}
And the second activity:
package com.test.scores;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageButton;
public class ResetImageResources extends Activity {
private ImageButton btn1, btn2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (ImageButton) findViewById(R.id.btn1);
btn2 = (ImageButton) findViewById(R.id.btn2);
btn1.setImageResource(R.drawable.btn01);
btn2.setImageResource(R.drawable.btn02);
finish();
}
}
Activities are absolutely isolated from each others. The same XML file which you are setting as a content of each activity doesn't mean that it same/shared instance of layout. You should think not in terms of layouts, but in terms of activities.
In your case you just start second Activity, change background of buttons here, then go back and see first Activity. Any changes in second Activity would not be mirrored somewhere else. That's it.
Try this:
Insert a button to finish in second activity. Use finish() under the button interface button.setOnClickListener(new OnClickListener(){} ); then you'll clearly notice the difference between the backgrounds. Only if you click you can go back to main activity.
I'm really new in android and java, but im trying to make an android app.
Im trying to make something were you can just type in your name and then it should view it by a push on a button. Eclipse is giving me the "unreachable code" error. I was wondering if you guys could see what i was doing wrong. The error is given in the two rules with final in front of it. If i remove one of these rules it will just move the error to the other rule.
Thank you in advance,
Marek
package com.tutorial.helloworld2;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
#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;
final EditText nameField = (EditText) findViewById(R.id.nameField);
final TextView nameView = (TextView) findViewById(R.id.nameView);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
nameView.setText("Hello " + nameField.getText());
}
});
}
}
Move return true to the end of method as the method execution will end on this statement and no code below will be executed.
it is due to return statement, return will return a true and exit the entire method you have currently written code in. you should write this in onCreate() like.
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
final EditText nameField = (EditText) findViewById(R.id.nameField);
final TextView nameView = (TextView) findViewById(R.id.nameView);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
nameView.setText("Hello " + nameField.getText());
}
});
}
now you will get your desired result
I am having trouble linking my interface with my java.
package com.example.game;
import android.os.Bundle;
import android.R;
import android.app.Activity;
import android.view.Menu;
import android.widget.Button;
public class MainActivity extends Activity {
Button btn1 = (Button) findViewById(R.id.button1);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); <---- 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); <--- main
return true;
}
}
I am having trouble with the layout. I can link my buttons ect. but it always throws an error on the generated code? why does it do this? I did the suggested fixes and ran it, which resulted into a crash... I am new to java and android development. I am learning as I go. I do have other experience in visual basic and what not.
Suggested fixes:
change to activity_list_ item
and can not be resolved.
Your btn1 assignment is incorrect. When you're assigning it on initialization, there is no view, as the code is run prior to onCreate. Instead, it should look like this:
Button btn1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); <---- activity_main
btn1 = (Button) findViewById(R.id.button1);
}
I am trying to add multiple buttons to my app so if you click one you automatically call a certain person, but now I'm stuck on the call-action, how can I make a call when there is being clicked on the button? I have the following code for my activity named telefoonnummers.java:
package com.example.rome;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;
import android.widget.EditText;
import android.widget.Button;
import android.view.View;
import android.widget.Toast;
public class Telefoonnummers extends Activity {
Button mHALbellen;
Button mWITbellen;
Button mWDGbellen;
Button mVlierbellen;
Button mHotelbellen;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_telefoonnummers);
mHALbellen = (Button) findViewById(R.id.button1);
mWITbellen = (Button) findViewById(R.id.button3);
mWDGbellen = (Button) findViewById(R.id.button4);
mVlierbellen = (Button) findViewById(R.id.button5);
mHotelbellen = (Button) findViewById(R.id.button6);
// Show the Up button in the action bar.
getActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_telefoonnummers, menu);
return 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);
}
public void vanHalbellen(View view){
if (view == mHALbellen){
//WHICH CODE SHOULD BE HERE TO MAKE A PHONECALL WHEN THE BUTTON mHALBELLEN IS PRESSED??
}
}
}
Would you guys please help???
Add the following permission to your manifest:
<uses-permission android:name="android.permission.CALL_PHONE" />
And then execute this Intent when the button is clicked:
String uri = "tel: phone_number_here";
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse(uri));
startActivity(intent);
You can also use Intent.ACTION_DIAL instead of Intent.ACTION_CALL. This shows the dialer with the number already entered, but allows the user to decide wether to actually make the call or not. ACTION_DIAL doesn't require the CALL_PHONE permission.
I have 2 classes for an Android project.
The first class is the Activity and the second class is just a OnClickListener which implements the interface.
If I run the project on my phone I always get an runtime error.
Also I got the message:
The specified activity does not exist! Getting the launcher activity.
Here are my two classes
SendActivity
package kops.sms;
//import android.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.Button;
public class SendActivity extends Activity {
Button buttonSend= (Button) findViewById(R.id.buttonSend);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_send);
buttonSend.setOnClickListener(new ButtonListener());
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.send, menu);
return true;
}
}
and the ButtonListener
package kops.sms;
import android.view.View;
import android.view.View.OnClickListener;
public class ButtonListener implements OnClickListener {
#Override
public void onClick(View v)
{
}
}
I donĀ“t know what is wrong...
I look forward to your replies! :)
You cannot call findViewById() until after you call setContentView(). Please move:
Button buttonSend= (Button) findViewById(R.id.buttonSend);
to after:
setContentView(R.layout.activity_send);
and before:
buttonSend.setOnClickListener(new ButtonListener());
Also, in the future, please use LogCat (e.g., in the DDMS perspective in Eclipse) to examine the Java stack trace associated with your crashes. You would have been told about your NullPointerException, and that may have helped you to fix your problem.
Be sure that your Activity is declared in your manifest. Also, change your onCreate()
public class SendActivity extends Activity {
Button buttonSend;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_send);
buttonSend = (Button) findViewById(R.id.buttonSend);
buttonSend.setOnClickListener(new ButtonListener());
}
You can't call a View such as a Button before you call setContentView() as it exists in your Layout and you haven't inflated your Layout until you call setContentViewe().
If these don't fix your problem then please post Logcat
Edit
Unless I missed it, you need to have all of your Activitys in your manifest. Something like:
<activity
android:name="your.package.name.SendActivity"
// activity attributes such as config changes, lable, etc...
</activity>
Logcat
Logcat output can be one of the most important pieces to determining a crash. It lists what the error was and a line number with activity where the problem occurred. If using Eclipse,
Window-->Show View-->Other-->Android-->Logcat
If you copy/paste the Logcat using coding brackets, it makes getting help much easier. You can also set filters for the logs so you don't get every single message and it is much more manageable. For example, I have a filter with: Filter Name: Runtime, by Log Tag: AndroidRuntime, by Log Level: error. This gives me only error messages for runtime errors/crashes. These filters are on the left side of the logcat view. Hope this helps