简体   繁体   English

Android,意图不起作用?

[英]Android, intent doesn't work?

This is my code.这是我的代码。 Its very simple but when I click on the button "button1", nothing happens.它非常简单,但是当我单击按钮“button1”时,什么也没有发生。 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.新 Logcat:11-29 12:15:39.552:D/gralloc_goldfish(613):未检测到 GPU 仿真的模拟器。 11-29 12:15:40.062: I/Choreographer(613): Skipped 51 frames! 11-29 12:15:40.062:I/Choreographer(613):跳过了51帧! The application may be doing too much work on its main thread.应用程序可能在其主线程上做了太多工作。 11-29 12:16:08.902: I/Choreographer(613): Skipped 30 frames! 11-29 12:16:08.902:I/Choreographer(613):跳过30帧! The application may be doing too much work on its main thread.应用程序可能在其主线程上做了太多工作。

You have to remove the globally declared Intent variable like this,您必须像这样删除全局声明的Intent变量,

 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.您的startActivity(i)可能已经引用了全局声明的Intent变量,您忘记初始化并在本地创建了一个与全局声明的名称相同的名称“i”的Intent对象。

If the above ideas didn't help you, then as the user Yajneshwar Mandal has suggested you need to try it.如果上述想法对您没有帮助,那么正如用户 Yajneshwar Mandal 所建议的那样,您需要尝试一下。 Or put a log inside the ClickListener to see if the control gets into it.或者在 ClickListener 里面放一个日志,看看控件是否进入其中。

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);

        }

    });
}


}

There might be two problems可能有两个问题

  1. you are declaring your intents twice, first globally and then locally.你两次声明你的意图,首先是全局的,然后是本地的。 The system might be referencing the global decleration.系统可能正在引用全局声明。

  2. Secondly, you have used其次,你用过

    button1.setOnCLickListener(new Button.OnClickListener() ... );

here you should use,在这里你应该使用,

button1.setOnClickListener(new View.OnClickListener()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM