首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >点击按钮时活动不会打开[ANDROID]

点击按钮时活动不会打开[ANDROID]
EN

Stack Overflow用户
提问于 2016-04-30 11:31:18
回答 4查看 44关注 0票数 0

我是android的新手,我想在点击主活动的SET通知按钮时调用一个名为SetNotification的活动,因为我已经编写了这段代码,但是当单击按钮时该活动不会打开。

MainActivity.java

代码语言:javascript
复制
package com.example.abc.project1;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    private static Button set_not;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

    public void onClickButtonListener() {
        set_not = (Button)findViewById(R.id.set_not_btn);
        set_not.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent("com.example.abc.project1.SetNotification");
                        startActivity(intent);
                    }
                }
        );
    }

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

AndroidManifest.xml

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.abc.project1">

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

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".SetNotification"
            android:label="@string/title_activity_set_notification"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="com.example.abc.project1.SetNotification" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest>

activity_set_notification.xml

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.example.abc.project1.SetNotification">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_set_notification" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>

误差

代码语言:javascript
复制
04-30 16:12:03.681 9001-9001/com.example.abc.project1 I/art: Not late-enabling -Xcheck:jni (already on)
04-30 16:12:03.681 9001-9001/com.example.abc.project1 I/art: Late-enabling JIT
04-30 16:12:03.690 9001-9001/com.example.abc.project1 I/art: JIT created with code_cache_capacity=2MB compile_threshold=1000
04-30 16:12:03.891 9001-9001/com.example.abc.project1 W/System: ClassLoader referenced unknown path: /data/app/com.example.abc.project1-2/lib/x86
04-30 16:12:04.232 9001-9008/com.example.abc.project1 W/art: Suspending all threads took: 36.566ms
04-30 16:12:04.650 9001-9008/com.example.abc.project1 W/art: Suspending all threads took: 48.347ms
04-30 16:12:04.836 9001-9028/com.example.abc.project1 D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
04-30 16:12:05.234 9001-9028/com.example.abc.project1 I/OpenGLRenderer: Initialized EGL, version 1.4
04-30 16:12:05.475 9001-9028/com.example.abc.project1 W/EGL_emulation: eglSurfaceAttrib not implemented
04-30 16:12:05.475 9001-9028/com.example.abc.project1 W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xacaf6420, error=EGL_SUCCESS
04-30 16:12:05.557 9001-9001/com.example.abc.project1 I/Choreographer: Skipped 30 frames!  The application may be doing too much work on its main thread.
04-30 16:12:06.275 9001-9001/com.example.abc.project1 I/Choreographer: Skipped 42 frames!  The application may be doing too much work on its main thread.
04-30 16:12:53.430 9001-9008/com.example.abc.project1 W/art: Suspending all threads took: 12.922ms
04-30 16:13:42.955 9001-9008/com.example.abc.project1 W/art: Suspending all threads took: 5.961ms
04-30 16:13:55.377 9001-9008/com.example.abc.project1 W/art: Suspending all threads took: 27.667ms
04-30 16:13:56.006 9001-9008/com.example.abc.project1 W/art: Suspending all threads took: 26.471ms
04-30 16:14:03.367 9001-9008/com.example.abc.project1 W/art: Suspending all threads took: 27.844ms
04-30 16:14:05.311 9001-9008/com.example.abc.project1 W/art: Suspending all threads took: 13.656ms
04-30 16:14:10.602 9001-9008/com.example.abc.project1 W/art: Suspending all threads took: 12.906ms
04-30 16:14:13.897 9001-9008/com.example.abc.project1 W/art: Suspending all threads took: 8.277ms
04-30 16:14:17.071 9001-9008/com.example.abc.project1 W/art: Suspending all threads took: 22.026ms
04-30 16:14:17.563 9001-9008/com.example.abc.project1 W/art: Suspending all threads took: 13.670ms
04-30 16:14:19.361 9001-9008/com.example.abc.project1 W/art: Suspending all threads took: 21.463ms
04-30 16:14:26.781 9001-9008/com.example.abc.project1 W/art: Suspending all threads took: 22.556ms
EN

回答 4

Stack Overflow用户

发布于 2016-04-30 11:35:12

你的意图应该是这样的:

代码语言:javascript
复制
Intent intent = new Intent(MainActivity.this, SetNotification.class);   
startActivity(intent);

顺便说一句,这些并不是必需的:

代码语言:javascript
复制
public void onClickButtonListener() {  }

只需将按钮和onclicklistener放在这些代码行之外,并删除这些onClickButtonListener行即可。

票数 1
EN

Stack Overflow用户

发布于 2016-04-30 11:35:32

试试这个:

代码语言:javascript
复制
Intent openActivity = new Intent(MainActivity.this, SetNotification.class);
startActivity(openActivity);
票数 1
EN

Stack Overflow用户

发布于 2016-04-30 11:36:20

尝试向new Intent()提供类而不是包名,如

Intent intent = new Intent(getActivity(), SetNotification.class); startActivity(intent);

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36954332

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档