I am new to programming.
I'm creating a simple app which will handle my Button click event.
I have added the button using xml and linked it to the program, but the app force closes immediately after I run it.
Here is the code:
public class Sparkling extends Activity implements OnClickListener
{
Button b;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b=(Button)findViewById(R.id.Button1);
b.setOnClickListener(this);
}
#Override
public void onClick(View v)
{
//actions....
}
}
Without having any stack trace info, my guess would be Button1 is missing from or is misspelled on main.xml or something is wrong with your manifest file.
I found out what had happened.
I was using:
android:name="#+id/Button1"
instead of:
android:id="#+id/Button1"
Related
Android Studio, Using Two Spinner (dropdown box) Select click Submit button open new activity page.
Give your Submit button an id.
android:id="#+id/submit" >>>In your xml file.
Create a variable before onCreate functions. >>>>In your java file.
public class Acces_Database extends AppCompatActivity {
Button buttonSub;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_acces__database);
buttonSub = (Button)findViewByid(R.id.submit);
buttonSub.setOnClickListener(new View.setOnClickListsener() {
#Override
public void onClick(View view){
startActivity(new Intent(Your_CurrentActivity_name.this,Your_NextActiivty_name.class));
}
});
Hope i answered your question ,if any irrelevancy ,please let me know.
Happy Coding!
i am trying to implement ShineButton in my project . I have successfully synced the library to the gradle and added shine button in the xml.
now when i am trying to write the java code
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b1 = (Button)findViewById(R.id.button);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), "Kill bill", Toast.LENGTH_SHORT).show();
}
});
ShineButton shineButton = (ShineButton) findViewById(R.id.po_image2);
shineButton.init(context);
}
}
.init(activity); is showing cannot resolve symbol activity.
You don't literally copy the code verbatim, you read the documentation and object types supported by the method.
public void init(Activity activity) {
For example, I assume you are running that from an activity based on the usage of findViewById? Then you need "this instance of the Activity"
shineButton.init(this);
or an instance of an Activity if you were in a Fragment
shineButton.init(getActivity());
If the code is in Activity use shineButton.init(Activityname.this).
If it is in
fragment use shineButton.init(getActivity()).
Read this: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/ and this https://developer.android.com/training/index.html
Change:
shineButton.init(context);
To:
shineButton.init(MainActivity.this);
MainActivity.this holds the instance of MainActivity class and can be used to initialise the view.
I have just started working with Butterknife library and written the following code:
class myActivity extends AppCompatActivity
{
#BindView(R.id.button) Button app1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
}
public void selectApp(View b)
{
Button clicked=(Button)b;
if(clicked==app1)
Toast.makeText(this,"First App clicked",Toast.LENGTH_LONG).show();
}
}
here selectApp is attached through onClick in the xml view file.
But the problem is clicked==app1 is returning false even when pressing app1. The method is being called but the if condition is coming false.
Can anybody clarify.
Thanks
I think this would work:
if(clicked.getId()==R.id.button)
Also, you can use View b and not parse into a button:
if(b.getId()==R.id.button)
¿Is this your actual code? seems to lack an annotation on the method.
I've been developing an Android App and I have encountered a small problem when putting my phone horizontally.
At the moment I have two views: The main one (main.xml) and a login view (login.xml). When I turn my phone while being in the login.xml view, it returns me to the main.xml view. The same thing happens if the phone is in horizontal position + login.xml and I turn it again (goes back to main.xml).
It sees that the "OnCreate" is executed each time, here is the code for the app:
public class AppActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageButton bout= null;
bout = (ImageButton)findViewById(R.id.imageButton1);
bout.setOnClickListener(boutlisten);
}
private OnClickListener boutlisten = new OnClickListener(){
public void onClick(View v) {
setContentView(R.layout.login);
}
};
}
Thanks in advance!
Create new Activity which have setContentView(R.layout.login); and start Activity.
Like,
public class LoginActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
}
}
See here
Addendum: This is the code to launch the activity declared above. One should also declare the activity in the manifest file
private OnClickListener boutlisten = new OnClickListener(){
public void onClick(View v) {
Intent myIntent = new Intent(v.getContext(), LoginActivity.class);
startActivity(myIntent);
}
};
Also, as u must already be aware of..don't forget to declare the new activity in your AndroidManifest.xml
<activity android:name=".LoginActivity" />
Alternatively, you may also force your activity to run just vertically. Just add this to your Main activity tag in AndroidManifest.xml
<activity android:screenOrientation="portrait" ...../>
I know that this topic has been already beaten enough, but I still don't understand completely if Android System has fine behavior in following case:
I created small app consists of two classes, here is the code:
Main.java
public class Main extends Activity {
private Button bv;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bv = (Button) findViewById(R.id.hello_txt);
bv.setOnClickListener(
new OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(Main.this, Main2.class);
startActivity(i);
}
}
);
}
}
Main2.java
public class Main2 extends Activity {
private TextView countOfActivities;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
countOfActivities = new TextView(this);
setContentView(countOfActivities);
countOfActivities.setText("Count of Activities: " + getInstanceCount());
}
}
When I clicked on the button from first activity several times, I get that even after pressing BACK button that should call second Activity's onDestroy() it's instance remains in the memmory.
Only after creating about 35 instances next click let me know, that GC cleared the memmory.
I just want to completely be sure that it is normal system's behavior.
Following pictures from Emulator and LogCat
Button clicked 10 times
LogCat output after clicked
Yes, the system works fine.
When you press the back button, your activity is removed from the activity stack.
onDestroy() may have been called, this doesn't mean that the instance was actually unallocated from the memory.