1

I have a hard time switching from one activity to another using intent. My app is almost done, I have there several activities and I am able to switch between them with intent. I have added one more activity to my project (in Eclipse: File ... New ... AndroidActivity). As I was used to, it created .java and .xml file. In my main activity, I have defined my button (xml) and in java class method to perform on buttonClick. Everything seems fine, but when I click my button nothing happens. What could be wrong?

Here is defined my main activity class called "Ponuka":

package com.example.s_home;

import android.os.Bundle;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;

public class Ponuka extends Activity  {

@Override
public void onCreate(Bundle savedInstanceState) {
    getWindow().setBackgroundDrawableResource(R.drawable.pozadie);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_ponuka);
    ActionBar actionBar = getActionBar();
    actionBar.hide();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_ponuka, menu);
    return true;
}

// switch to activity: Osvetlenie  --- works fine
public void obrOsv(View view) {
    Intent intent = new Intent (this, Osvetlenie.class);
    startActivity(intent);  
}

 // switch to activity: Kurenie   --- works fine
 public void obrKur(View view) {
    Intent intent = new Intent (this, Kurenie.class);
    startActivity(intent);
}

 // switch to activity: Ochrana   --- this isnt working
 public void obrBez(View view) {
    Intent intent = new Intent (this, Ochrana.class);
    startActivity(intent);
 System.out.println("ok");           // It will not even print out "ok" 
}

}

Here is activity_ponuka.xml :

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="150dp"
>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/lin1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
>

<Button
     android:id="@+id/bezpecnost"              <!-- THIS IS THE BUTTON -->
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:background="@drawable/zabezp"
      android:onClick="obrBez"

      />

<Button
    android:id="@+id/kuren"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
   android:layout_marginLeft="10dp"
    android:background="@drawable/kurenie"

    android:onClick="obrKur" />

 </LinearLayout>

 <LinearLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
     android:id="@+id/lin2"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:layout_marginTop="90dp"
     android:gravity="center"
     android:orientation="horizontal" >

  <Button
    android:id="@+id/osvetl"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/osvetlenie"
    android:onClick="obrOsv"

        />

   <Button
    android:id="@+id/pohod"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/pohodlie"
    android:layout_marginLeft="10dp"

     />
     </LinearLayout>
 </RelativeLayout>

One more note: When my new activity called "Ochrana" was created, I had to manually update the R.java file. What did I do wrong here?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
user2179427
  • 331
  • 5
  • 16
  • Did you define your second activity in your manifest file? – ACengiz Apr 27 '13 at 21:56
  • It should work, why that button without syntax highlight? maybe it's just commented. Another way - create reference to button in java code and specify the onclicklistener but remove "onclick" attribute. – Mickey Tin Apr 27 '13 at 21:57

6 Answers6

0

You shouldn't use System.out.println on Android, rather log the message using the Log class, then you can find it in LogCat. System.out.println might get redirected to LogCat too, but it's not guaranteed. Why doesn't "System.out.println" work in Android?

Is your problem that it starts a different activity than you want or doesn't it start one at all? Because you're creating an intent with Kurenie.class where you obviously want to start the Ochrana activity (according to the comment above the obrBez() method).

PS: Zdravim z Brna. :)

Community
  • 1
  • 1
zbr
  • 6,860
  • 4
  • 31
  • 43
  • He don't need to call this method, "onclick" attribute handle method invoking – Mickey Tin Apr 27 '13 at 21:58
  • Yeah, I noticed that too after posting. Still not used to this way of implementing it. Thank you. – zbr Apr 27 '13 at 22:01
  • Zabri - my fault. In my source code, I am calling Ochrana.class and It doesnt start at all. And if I add that system.out.println on other buttons, it works fine. So I guess my problem is that Button. But what could it be? – user2179427 Apr 27 '13 at 22:08
0

You should never manually update the R.java because it automatically updates itself if your code is correct.

You also need to get an instance of your button in your activity and add a OnClickListener to call the methods that switch the activity.

And the last thing you need to do is to add those activities you want to use in your manifest file.

Bongoking
  • 3
  • 2
  • I know, that it should update automatically, that is why I am wondering. In the rest of my code, I have no errors so I simply dont understand what did i do. Now to your second paragraph. I made my other buttons exactly the same way and they work no prob. – user2179427 Apr 27 '13 at 22:12
0

Probably unrelated to your issue but you're calling the wrong Activity in your new button event:

public void obrBez(View view) {
    Intent intent = new Intent (this, Kurenie.class);
GreenCoder
  • 56
  • 3
0

I would recommend using setOnClickListener() for the buttons instead of the android:onClick in the XML - XML ment for layout and design and java activity files ment for logic, mixing the two can cause confusion. You can also try that for solving your problem (Although I don't see any problem with your code):

Button btn = (Button) findViewById(R.id.bezpecnost);
btn.setOnClickListener(new OnClickListener() {
    @Override
        public void onClick(View v) {
             //Assuming you want Ochrana activity from your comment
             Intent intent = new Intent (this, Ochrana.class); 
             startActivity(intent);
        }
    });

Two more things that won't solve your problem but might help you: You might want to define Intent intent; for the entire activity class and than create new intent on the same variable to save memory allocating. And last thing - give your XML file [Ctrl]+A and than [Ctrl]+i to automatically order the spacing - it will do only good (:

P.S. If you have any problems with your R file - just delete it! Eclipse auto re-create it immediately - solving all kinds of R bad/not updated...

Aviel Gross
  • 9,770
  • 3
  • 52
  • 62
  • should I delete the whole R.java? – user2179427 Apr 27 '13 at 22:44
  • yes. It will ask you if you sure you want to delete it, don't worry it will be immediately created again. If it doesn't - close Eclipse and reopen it - and welcome to the big world of Android ADT bugs (: – Aviel Gross Apr 28 '13 at 05:59
0

The answer is in your XML layout.... look closely...

<Button
     android:id="@+id/bezpecnost"              <!-- THIS IS THE BUTTON -->
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:background="@drawable/zabezp"
      android:onClick="obrBez"

      />

The comment should rather be in this format <!-- .... //-->

You left out the two forward slashes - that's why nothing is happening!

Edit:

Eclipse would have shown the entire block commented out highlighted in colour green instead of XML syntax highlight colour!

In regards to R.java - that is generated at compile time, so whenever you make changes to your XML layouts, the Android SDK will re-generate the R.java file each time!

The AndroidManifest.xml should have the lines specifying the activity within the tags application, as in:

<activity android:name="com.example.s_home.Ochrana" 
          android:label="Ochrana Activity"/>
t0mm13b
  • 34,087
  • 8
  • 78
  • 110
  • I have added this comment only here to let you know where is that particular button that doesnt work. I dont have this in my source code. – user2179427 Apr 27 '13 at 22:22
  • ahhh okie... fair enough... :) – t0mm13b Apr 27 '13 at 22:26
  • @user217947 - what does the logcat say? – t0mm13b Apr 27 '13 at 22:27
  • nothing. When I click that button nothing happen, and logcat is "quiet" :). When I click other buttons everything seems working fine, but that R file isnt regenerating for me. I have deleted what I have manually added. But in this case project is showing some errors in my new activity class: setContentView(R.layout.activity_ochrana); – user2179427 Apr 27 '13 at 22:41
  • logcat is quiet because you have not used `Log.d("...", "...");` to see the code-flow... :) – t0mm13b Apr 27 '13 at 22:48
0

Your xml is all wrong. Your button with "@+id/pohod" is mixed up with button "@+id/bezpecnost" because you missed android:layout_below="@id/lin1" in your second linear layout. I tested the layout below and it is OK. I change to android:text because I have no drawable.

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="150dp"
>

<LinearLayout 
android:id="@+id/lin1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
>

<Button
     android:id="@+id/bezpecnost"             
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="Ochrana"
      android:onClick="obrBez"

      />

<Button
    android:id="@+id/kuren"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
   android:layout_marginLeft="10dp"
    android:text="Kurenie"

    android:onClick="obrKur" />

 </LinearLayout>

 <LinearLayout

     android:id="@+id/lin2"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:layout_marginTop="90dp"
     android:gravity="center"
     android:layout_below="@id/lin1"
     android:orientation="horizontal" >

  <Button
    android:id="@+id/osvetl"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="osvetlenie"
    android:onClick="obrOsv"

        />

   <Button
    android:id="@+id/pohod"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="pohodlie"
    android:layout_marginLeft="10dp"

     />
     </LinearLayout>
 </RelativeLayout>
Hoan Nguyen
  • 18,033
  • 3
  • 50
  • 54