【问题标题】:cannot open another activity with a button无法使用按钮打开另一个活动
【发布时间】:2015-03-28 03:17:55
【问题描述】:

因此,当此代码运行时,我没有收到任何错误,它会正确显示按钮和所有内容,但是当我单击按钮时,没有任何反应,它只是单击并单击,然后单击下面没有任何反应,我将发布 XML 清单代码,以便您可以看到我已经完美地完成了所有事情,但不知何故,它没有做我要求它做的正确事情。我已经尝试了我多次问过这个问题的所有事情,每次回答的人都错了我会用最少的代码发布我的整个新项目,只有 3 个类我认为我的设置可能有问题,因为这在我的两台计算机上都不起作用这里是主类代码或“启动器”谢谢大家的帮助,祝你有美好的一天。

    package com.tripp.funnystuff;

    import android.os.Bundle;
    import android.support.v7.app.ActionBarActivity;
    import android.view.Menu;
    import android.view.MenuItem;


     public class Main extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide();
        setContentView(R.layout.main);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

这里是菜单类

package com.tripp.funnystuff;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

/**
 * Created by Devin on 3/27/2015.
 */
public class Menu extends Activity {

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

        //setting up the button references
        Button jokeD = (Button) findViewById(R.id.jokeoftheday);
        //Button jokeC = (Button) findViewById(R.id.jokecatagories);

        jokeD.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                Intent i = new Intent(Menu.this, JokeOfTheDay.class);


                startActivity(i);

            }
        });

        /*jokeC.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent s = new Intent(Menu.this, JokeCatagories.class);

                Menu.this.startActivity(s);

            }
        });
        */

    }

    @Override
    protected void onPause() {

        // TODO Auto-generated method stub
        super.onPause();
    }
}

这是一天班的笑话

package com.tripp.funnystuff;

import android.app.Activity;
import android.os.Bundle;

/**
 * Created by Devin on 3/27/2015.
 */
public class JokeOfTheDay extends Activity {

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

这里是xml主要代码

<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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"  


    tools:context=".Main">


    <Button
        android:id="@+id/jokeoftheday"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:textStyle="bold"
        android:text="joke of the day"/>

    </RelativeLayout>

下面是今日笑话的 xml 代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="Small Text"
        android:id="@+id/textView"
        android:layout_gravity="center_horizontal" />
</LinearLayout>

最后,但并非最不重要的是清单代码

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.tripp.funnystuff" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".Main"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Menu"
            android:label="@string/app_name" >
        </activity>
        <activity
            android:name=".JokeOfTheDay"
            android:label="@string/app_name" >
        </activity>
    </application>

</manifest>

只有当您想编辑我的英语时,我才需要认真询问,然后我将取消编辑它我需要帮助我的代码而不是我的英语。所以带着你的****去别的地方。我在我的电脑上花了一个半星期的时间在互联网上寻找这个问题,如果有人遇到过同样的问题,请给我一些关于如何解决它的线索。我真的很感谢所有读过这篇文章的人,以及所有愿意花时间帮助我的聪明程序员。谢谢!

【问题讨论】:

  • 这段代码在控制台中确实没有错误,但是在 log cat 中有一个 egl_blah_blah 错误,如果这与程序运行有任何关系,并且一切仍然存在
  • 03-27 21:50:26.288 3027-3042/? W/EGL_emulation:eglSurfaceAttrib 未实现 03-27 21:50:26.288 3027-3042/? W/OpenGLRenderer:无法在表面 0xae0fffe0 上设置 EGL_SWAP_BEHAVIOR,错误=EGL_SUCCESS
  • 03-27 21:50:25.615 3017-3017/? E/memtrack:无法加载memtrack模块(没有这样的文件或目录)03-27 21:50:25.615 3017-3017/? E/android.os.Debug:加载memtrack模块失败:-2 03-27 21:50:25.647 3017-3026/? E/art:运行时关闭时附加线程:Binder_1 @Hulk
  • 这三个类有什么关系?我没有看到 Main 类与 Menu 类有什么关系?

标签: android xml android-activity


【解决方案1】:

您的 Main.java 正在显示按钮,但没有单击侦听器。 Menu.java 是不必要的,你真的应该看看开发者的网站示例。

【讨论】:

  • 但是 Menu.java 不应该仍然监听点击并且活动应该仍然运行吗?
  • 哇一直......就是这样......微尘你知道你从我身上解除的痛苦
  • @DevinTripp 不客气。我们都去过那里。如果您刚刚开始,有几个提示:1) 使用代码格式化程序。将其设置为在保存时运行或手动运行;) 2) 除非导入有助于澄清 Date 之类的使用,否则将它们排除在 SO 帖子之外。除非它与问题相关,否则您的包裹也是如此。 3) 最后,使用 Lint。它并不完美,但它是代码中气味测试的一个很好的起点。最终,您将希望使用更多工具,如 FindBugs、PMD、CheckStyle 等。goo.gl/zR0YvI
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-10
  • 2014-12-24
  • 2017-12-17
  • 1970-01-01
相关资源
最近更新 更多