我是Android新手,我想在主活动中点击SET NOTIFICATION按钮时调用名为 SetNotification 的活动,因为我已编写此代码,但活动未打开时点击按钮。
MainActivity.java
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
<?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
<?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>
错误
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
答案 0 :(得分:1)
你的意图应该是这样的:
Intent intent = new Intent(MainActivity.this, SetNotification.class);
startActivity(intent);
顺便说一下,这些都不是必需的:
public void onClickButtonListener() { }
只需将您的按钮和onclicklistener放在这些代码行之外,然后删除onClickButtonListener
答案 1 :(得分:1)
试试这个:
Intent openActivity = new Intent(MainActivity.this, SetNotification.class);
startActivity(openActivity);
答案 2 :(得分:1)
尝试向new Intent()
提供类而不是包名称
Intent intent = new Intent(getActivity(), SetNotification.class);
startActivity(intent);
答案 3 :(得分:1)
你打开活动的方式是错误的。试试这种方式。
set_not.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,SetNotification.class);
startActivity(intent);
}
}
);