所以我试图启动我的第二个活动,即" dummy.class"
其代码在这里:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
public class dummy extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dummy);
}
}
我在MainActivity中单击此代码生成的通知时尝试执行此操作:
public class MainActivity extends AppCompatActivity
{
int notifyId = 1;
int messages = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b1 = (Button) findViewById(R.id.gen_notif);
}
public void createNotif(View v)
{
messages += 1;
notifyId += 1 ;
NotificationManager notificationManager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Intent intent=new Intent(MainActivity.this ,dummy.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,(int) System.currentTimeMillis(),intent,0);
NotificationCompat.Builder n = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
.setContentTitle("Hello")
.setContentText("Hello World Notification")
.setContentIntent(pendingIntent)
.setSmallIcon(R.mipmap.ic_launcher)
.setNumber(messages)
.setAutoCancel(true);
notificationManager.notify(notifyId,n.build());
}
以下是activity_main的XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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"
tools:context="com.manan.notif.MainActivity">
<Button
android:id="@+id/gen_notif"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/b_text"
android:onClick="createNotif"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
然而,当我点击通知时它什么都不做(除了由于代码而解雇自己)。我只是在学习,所以我不知道自己做错了什么,可以用另一双眼睛。
答案 0 :(得分:0)
Ok这是一个愚蠢的错误,就像我想的那样,我忘了将新类作为活动添加到清单文件中,只需添加此修复它:
<activity android:name="dummy"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
</intent-filter>
</activity>