Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android, intent doesn't work?

This is my code. Its very simple but when I click on the button "button1", nothing happens. What did I do wrong?

public class TestTab extends Activity {

    Button button1;
        Intent i;


    @Override

    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        button1 = (Button) findViewById(R.id.btnScore);
        button1.setOnClickListener(new View.OnClickListener() {


            @Override
            public void onClick(View v) {

                Toast.makeText(getApplicationContext(), "On Click Event", 5000).show();

                //i = new Intent(TestTab.this, AndroidTab.class);
                //startActivity(i);

            }

        });
   }}

second--

package com.example.picturegame2;

import android.app.TabActivity;


public class AndroidTab extends TabActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tabzz);

        TabHost tabHost = getTabHost();

        TabSpec localspec = tabHost.newTabSpec("Local");
        localspec.setIndicator("Local", getResources().getDrawable(R.drawable.friend));
        Intent localIntent = new Intent(this, local.class);
        localspec.setContent(localIntent);

        TabSpec worldwidespec = tabHost.newTabSpec("worldwide");
        worldwidespec.setIndicator("Worldwide", getResources().getDrawable(R.drawable.worldwide));
        Intent worldwideIntent = new Intent(this, worldwide.class);
        worldwidespec.setContent(worldwideIntent);

        tabHost.addTab(worldwidespec);
        tabHost.addTab(localspec);



    }


}

third--

package="com.example.picturegame2"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="15" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/title_activity_main" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".local"/>
    <activity android:name=".AndroidTab"/>
    <activity android:name=".TestTab"/>
    <activity android:name=".tabzz"/>
    <activity android:name=".worldwide"/>
</application>

layout--

<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" >

    <Button
        android:layout_width="250dp"
        android:layout_height="100dp"
        android:text="Start New Game"
        android:id="@+id/BtnGame"
        android:textSize="35dp"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        />

    <ListView 
        android:id="@+id/LsOpenGames"
        android:layout_width="250dp"
        android:layout_height="210dp"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="130dp" 

        ></ListView>

    <Button
        android:layout_width="120dp"
        android:layout_height="40dp"
        android:text="Profile"
        android:id="@+id/btnProfile"
        android:textSize="15dp"
        android:layout_marginLeft="40dp"
        android:layout_marginTop="375dp"
        />

    <Button
        android:layout_width="120dp"
        android:layout_height="40dp"
        android:text="Score"
        android:id="@+id/btnScore"
        android:textSize="15dp"
        android:layout_marginLeft="170dp"
        android:layout_marginTop="375dp"
        />

</RelativeLayout>

New Logcat: 11-29 12:15:39.552: D/gralloc_goldfish(613): Emulator without GPU emulation detected. 11-29 12:15:40.062: I/Choreographer(613): Skipped 51 frames! The application may be doing too much work on its main thread. 11-29 12:16:08.902: I/Choreographer(613): Skipped 30 frames! The application may be doing too much work on its main thread.

like image 694
Rik Avatar asked Dec 26 '22 14:12

Rik


2 Answers

You have to remove the globally declared Intent variable like this,

 Button button1;
 @Override

    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        button1 = (Button) findViewById(R.id.btnScore);
        button1.setOnClickListener(new Button.OnClickListener() {

        @Override
        public void onClick(View v) {

         Intent i = new Intent(TestTab.this, AndroidTab.class);
         startActivity(i);

            }

        });
    }

Or try this,

     Button button1;
     Intent i;


@Override

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    button1 = (Button) findViewById(R.id.btnScore);
    button1.setOnClickListener(new Button.OnClickListener() {

    @Override
    public void onClick(View v) {

     i = new Intent(TestTab.this, AndroidTab.class);
     startActivity(i);

        }

    });
}

Your startActivity(i) could have been referencing the globally declared Intent variable which you forgot to initialize and created a Intent object locally with the same name "i" as of the globally declared one.

If the above ideas didn't help you, then as the user Yajneshwar Mandal has suggested you need to try it. Or put a log inside the ClickListener to see if the control gets into it.

like image 157
Andro Selva Avatar answered Jan 15 '23 10:01

Andro Selva


public class TestTab extends Activity {

Button button1;

@Override

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    button1 = (Button) findViewById(R.id.btnScore);
    button1.setOnClickListener(new Button.OnClickListener() {

    @Override
    public void onClick(View v) {
    // TODO Auto-generated method stub
    Intent i = new Intent(TestTab.this, AndroidTab.class);
    //i.setAction(Intent.ACTION_CALL_BUTTON);
    startActivity(i);

        }

    });
}


}
like image 22
Android Developer Avatar answered Jan 15 '23 09:01

Android Developer