对于名称类需要完整路径,并不能发现只有类的名字 - 机器人并不能、机器人、路径、名字

2023-09-12 22:25:55 作者:坐看云起时

我试图使用的Class.forName()方法,我的文件只是名字。

在我的情况下,它是MainActivity但是,当我尝试使用它在try catch块就找不到类与MainActivity的名字,当我调试,我发现

MainActivity.class.getName()给我的 com.example.test.secondproject.MainActivity

所以我用它作为我的参数的Class.forName(com.example.test.secondproject.MainActivity)和它的工作就好了,

如何才能使其与刚刚的Class.forName(MainActivity)的工作,或者是预期的行为

 尝试{
            Log.i(TAG,试主要活动); showNotification(背景下,intent.getStringExtra(信息)的toString()的Class.forName(MainActivity));


        }赶上(ClassNotFoundException异常E){
            Log.i(TAG,尝试失败的错误:+ E);

        }
 

这是错误:抛出java.lang.ClassNotFoundException:MainActivity

showNotification;

 公共静态无效showNotification(上下文的背景下,字符串消息,final类ActivityToOpen){
        PendingIntent contentIntent = PendingIntent.getActivity(上下文,0,
                新意图(上下文,ActivityToOpen),0);



        NotificationCompat.Builder mBuilder =
                新NotificationCompat.Builder(上下文)
                        .setSmallIcon(R.drawable.ic_launcher)
                        .setContentTitle(我的通知)
                        .setContentText(消息);
        mBuilder.setContentIntent(co​​ntentIntent);
        mBuilder.setDefaults(Notification.DEFAULT_SOUND);
        mBuilder.setAutoCancel(真正的);
        NotificationManager mNotificationManager =
                (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(1,mBuilder.build());

    }
 
史上最强盘点 说说配送机器人那些事儿

解决方案

如果您正在使用的所有类相同的包,那么你可以设置为常数,并追加到类。否则,你就需要指定全包的名称。

I'm trying to use Class.forName() method with just the name of my file.

In my case it's "MainActivity" But when I try to use it in a try catch block it cant find the Class with name of MainActivity, when i debugged, i found out that

MainActivity.class.getName() gives me com.example.test.secondproject.MainActivity

so i used it as my argument in Class.forName("com.example.test.secondproject.MainActivity") and it worked just fine,

how can I make it work with just Class.forName("MainActivity"), or is it an expected behavior

  try {
            Log.i(TAG,"try main activity ");            showNotification(context,intent.getStringExtra("message").toString(),Class.forName("MainActivity"));


        } catch (ClassNotFoundException e) {
            Log.i(TAG,"try fail error: "+ e);

        }

this is the error: java.lang.ClassNotFoundException: MainActivity

showNotification;

 public static void  showNotification(Context context,String message,final Class ActivityToOpen) {
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
                new Intent(context, ActivityToOpen), 0);



        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(context)
                        .setSmallIcon(R.drawable.ic_launcher)
                        .setContentTitle("My notification")
                        .setContentText(message);
        mBuilder.setContentIntent(contentIntent);
        mBuilder.setDefaults(Notification.DEFAULT_SOUND);
        mBuilder.setAutoCancel(true);
        NotificationManager mNotificationManager =
                (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(1, mBuilder.build());

    }

解决方案

If you are using the same package for all classes then you can set that as a constant and append to your class. Otherwise you will need to specify the whole package name.