【问题标题】:Android, intent doesn't work?Android,意图不起作用?
【发布时间】:2012-11-26 09:54:16
【问题描述】:

这是我的代码。它非常简单,但是当我单击按钮“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 帧!应用程序可能在其主线程上做了太多工作。

【问题讨论】:

  • logcat有异常吗?您是否在清单文件中添加了新活动?
  • Y 有两个 Intent i 语句吗?
  • 能否请您发布您的 AndroidManifest.xml 文件和 logact 也

标签: android button android-intent tabs android-tabhost


【解决方案1】:

你必须像这样删除全局声明的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 中放置一个日志,以查看控件是否进入其中。

【讨论】:

  • 但我认为这不是原因。在这种情况下 startActivity(i);始终从本地意图 I 中获取价值。如果您声明一个具有相同名称 golbaly 的 Intent ,我认为它不会影响 startActivity(i); . Manifest/新 Activity 类中的问题。
  • 我测试过他的代码。 (两个意图全局和本地)。它工作正常。请发布清单和 AndroidTab 。那么就很清楚了。但是无论你说什么都不是我想的原因。
  • 可能需要检查 AndroidTab 类是否有扩展的 Activity。不是吗? Bcoz 如果他没有在清单中声明它,那么它肯定会崩溃。如果根本没有这样的类,那么他可能会遇到编译时错误。
  • 他在 Androidmanifest 中犯了错误。他没有在清单中声明 Activity
  • 但是应用程序会崩溃吗?
【解决方案2】:
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);

        }

    });
}


}

【讨论】:

    【解决方案3】:

    可能有两个问题

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

    2. 其次, 你用过

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

    你应该在这里使用,

    button1.setOnClickListener(new View.OnClickListener()
    

    【讨论】:

    • 您是否尝试过使用 View.onClickListener() 代替 Button.onClickListener()**。?还有你面临的问题是什么。有什么错误或其他的吗?
    • 是的,我试过了,我没有任何错误,当我点击按钮时,它什么也没做
    • 交叉检查您在类和 xml 中提到的按钮是否与您正在单击的按钮相同。还可以尝试打印一些日志以检查 onClick 事件是否有效。
    • 我不知道如何检查 onclick?
    • 只需在 onCLick 事件中打印一些吐司。 Toast.makeText(getApplicationContext(), "点击事件", 5000).show();
    猜你喜欢
    • 2011-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-16
    • 1970-01-01
    • 1970-01-01
    • 2021-04-18
    • 2015-10-18
    相关资源
    最近更新 更多