问题描述
在简历中,在第一个活动中,我有一个登录按钮,当我单击它时,我想调用另一个项目的第二个主要活动。
这是我第一个活动的清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapplication_login"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.enamul.qrcode"
>
<application
android:allowBackup="true"
android:icon="@drawable/logo"
android:label="@string/app_name"
android:roundIcon="@drawable/logo"
android:supportsRtl="true"
android:theme="@style/Theme.MyApplication_login">
<activity
android:name=".ui.login.LoginActivity"
android:label="program">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.enamul.qrcode.MainActivity">
<intent-filter>
<action android:name="com.example.myapplication_login"></action>
</intent-filter>
</activity>
</application>
</manifest>
我的第二个清单是:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.enamul.qrcode">
<uses-permission android:name="android.permission.CAMERA" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity"
android:configChanges="orientation|screenSize"
android:windowSoftInputMode="adjustPan">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
在我的第一个清单中有一个错误:在项目或库中找不到清单com.example.enamul.qrcode.MainActivity中引用的类
我该如何解决?有人可以帮助我吗?
loginButton.setonClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(LoginActivity.this,com.example.enamul.qrcode.MainActivity);
startActivity(intent);
}
谢谢你们。
解决方法
您应该使用Intent。像这样
Uri uri = Uri.parse("http://instagram.com/_u/xxx");
Intent likeIng = new Intent(Intent.ACTION_VIEW,uri);
likeIng.setPackage("com.instagram.android");
try {
startActivity(likeIng);
} catch (ActivityNotFoundException e) {
startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse("http://instagram.com/xxx")));
}
,
您有两个选择:
首先是您要打开的应用程序的软件包名称:
button.setOnClickListener(view -> {
// intent will be null if no intent is found for the package
Intent intent = getPackageManager().getLaunchIntentForPackage("com.example.enamul.qrcode");
startActivity(intent);
});
第二步是定义您的活动将解决的问题。对于要打开的活动,添加一个定义方案和主机的意图过滤器:
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:scheme="something"
android:host="somethingelse" />
</intent-filter>
</activity>
要在按钮上打开它,请从另一个应用程序中单击:
button.setOnClickListener(view -> {
Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse("something://somethingelse"));
startActivity(intent);
});
尽管您应该使用对您的应用更为明智的主机和方案。