4

这是我的代码。它非常简单,但是当我单击按钮“button1”时,什么也没有发生。我做错了什么?

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

            }

        });
   }}

第二 -

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



    }


}

第三 -

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>

布局 -

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

新 Logcat:11-29 12:15:39.552:D/gralloc_goldfish(613):没有检测到 GPU 仿真的仿真器。11-29 12:15:40.062:I/Choreographer(613):跳过 51 帧!应用程序可能在其主线程上做了太多工作。11-29 12:16:08.902: I/Choreographer(613): 跳过 30 帧!应用程序可能在其主线程上做了太多工作。

4

3 回答 3

5

您必须像这样删除全局声明的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);

            }

        });
    }

或者试试这个,

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

        }

    });
}

startActivity(i)可能一直在引用您忘记初始化的全局声明的Intent变量,并在本地创建了一个与全局声明的同名“i”的Intent对象。

如果上述想法对您没有帮助,那么正如用户 Yajneshwar Mandal 所建议的,您需要尝试一下。或者在 ClickListener 中放置一个日志,以查看控件是否进入其中。

于 2012-11-26T09:59:21.967 回答
3
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);

        }

    });
}


}
于 2012-11-26T10:00:21.643 回答
2

可能有两个问题

  1. 您将两次声明您的意图,首先是全局,然后是本地。系统可能正在引用全局减速。

  2. 其次,你用过

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

在这里你应该使用,

button1.setOnClickListener(new View.OnClickListener()
于 2012-11-26T10:18:28.413 回答