C# “缺少组件”;主要活动“;沙马林机器人
我正在努力完成我的申请。由于我有一个PCL App.cs,我试图将该代码添加到我的C# “缺少组件”;主要活动“;沙马林机器人,c#,android,xamarin,android-intent,xamarin.android,C#,Android,Xamarin,Android Intent,Xamarin.android,我正在努力完成我的申请。由于我有一个PCL App.cs,我试图将该代码添加到我的MainActivity.cs.但是,当我构建项目时,我的MainActivity似乎缺失或者我的清单不正确 MainActivity.cs using System; using Android.App; using Android.Content; using Android.Content.PM; using Android.Runtime; using Android.Views; using Andr
MainActivity.cs.
但是,当我构建项目时,我的MainActivity
似乎缺失或者我的清单不正确
MainActivity.cs
using System;
using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Plugin.Messaging;
using Plugin.Permissions;
using Plugin.Permissions.Abstractions;
using System.Threading.Tasks;
namespace Divco.Droid
{
[Activity(Label = "Divco.Droid", Icon = "@drawable/logo_size_icon", Theme = "@style/MyTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
public event EventHandler<ServiceConnectedEventArgs> LocationServiceConnected = delegate { };
protected static LocationServiceConnection locationServiceConnection;
public static MainActivity Current
{
get { return current; }
}
private static MainActivity current;
public LocationService LocationService
{
get
{
if (locationServiceConnection.Binder == null)
throw new Exception("Service not bound yet");
// note that we use the ServiceConnection to get the Binder, and the Binder to get the Service here
return locationServiceConnection.Binder.Service;
}
}
protected override void OnCreate(Bundle bundle)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(bundle);
global::Xamarin.Forms.Forms.Init(this, bundle);
Xamarin.FormsMaps.Init(this, bundle);
CrossMessaging.Current.Settings().Phone.AutoDial = true;
LoadApplication(new App());
MainActivity.StartLocationService();
}
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
}
//public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)
//{
// PermissionsImplementation.Current.OnRequestPermissionsResult(requestCode, permissions, grantResults);
//}
public static void StartLocationService()
{
// create a new service connection so we can get a binder to the service
locationServiceConnection = new LocationServiceConnection(null);
// this event will fire when the Service connectin in the OnServiceConnected call
locationServiceConnection.ServiceConnected += (object sender, ServiceConnectedEventArgs e) => {
Console.WriteLine("Service Connected");
};
// Starting a service like this is blocking, so we want to do it on a background thread
new Task(() => {
// Start our main service
Console.WriteLine("App", "Calling StartService");
Android.App.Application.Context.StartService(new Intent(Android.App.Application.Context, typeof(LocationService)));
// bind our service (Android goes and finds the running service by type, and puts a reference
// on the binder to that service)
// The Intent tells the OS where to find our Service (the Context) and the Type of Service
// we're looking for (LocationService)
Intent locationServiceIntent = new Intent(Android.App.Application.Context, typeof(LocationService));
Console.WriteLine("App", "Calling service binding");
// Finally, we can bind to the Service using our Intent and the ServiceConnection we
// created in a previous step.
Android.App.Application.Context.BindService(locationServiceIntent, locationServiceConnection, Bind.AutoCreate);
}).Start();
}
public static void StopLocationService()
{
// Check for nulls in case StartLocationService task has not yet completed.
Console.WriteLine("App", "StopLocationService");
// Unbind from the LocationService; otherwise, StopSelf (below) will not work:
if (locationServiceConnection != null)
{
Console.WriteLine("App", "Unbinding from LocationService");
Android.App.Application.Context.UnbindService(locationServiceConnection);
}
// Stop the LocationService:
if (Current.LocationService != null)
{
Console.WriteLine("App", "Stopping the LocationService");
Current.LocationService.StopSelf();
}
}
}
}
使用系统;
使用Android.App;
使用Android.Content;
使用Android.Content.PM;
使用Android.Runtime;
使用Android.Views;
使用Android.Widget;
使用Android.OS;
使用Plugin.Messaging;
使用Plugin.Permissions;
使用Plugin.Permissions.abstraction;
使用System.Threading.Tasks;
名称空间Divco.Droid
{
[活动(Label=“Divco.Droid”,Icon=“@drawable/logo\u size\u Icon”,Theme=“@style/MyTheme”,MainLauncher=true,ConfigurationChanges=ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
公共类MainActivity:全局::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
public EventHandler LocationServiceConnected=委托{};
受保护的静态位置ServiceConnection位置ServiceConnection;
公共静态电流
{
获取{返回当前;}
}
私有静态电流;
公共场所服务场所服务
{
得到
{
if(locationServiceConnection.Binder==null)
抛出新异常(“尚未绑定服务”);
//请注意,我们在这里使用ServiceConnection来获取活页夹,使用活页夹来获取服务
返回位置serviceconnection.Binder.Service;
}
}
创建时受保护的覆盖无效(捆绑包)
{
TabLayoutResource=Resource.Layout.Tabbar;
ToolbarResource=Resource.Layout.Toolbar;
base.OnCreate(bundle);
全局::Xamarin.Forms.Forms.Init(这个,bundle);
Xamarin.formsmap.Init(this,bundle);
CrossMessaging.Current.Settings().Phone.AutoDial=true;
加载应用程序(新应用程序());
MainActivity.StartLocationService();
}
受保护的覆盖void OnActivityResult(int请求代码、结果代码、意图数据)
{
base.OnActivityResult(请求代码、结果代码、数据);
}
//public override void OnRequestPermissionsResult(int-requestCode,字符串[]权限,权限[]grantResults)
//{
//PermissionsImplementation.Current.OnRequestPermissionsResult(请求代码、权限、grantResults);
//}
公共静态定位服务()
{
//创建一个新的服务连接,这样我们就可以获得该服务的绑定
locationServiceConnection=新locationServiceConnection(null);
//当OnServiceConnected调用中的服务连接时,将触发此事件
locationServiceConnection.ServiceConnected+=(对象发送方,ServiceConnectedEventTargets e)=>{
控制台写入线(“服务连接”);
};
//启动这样的服务是阻塞的,所以我们希望在后台线程上执行
新任务(()=>{
//开始我们的主要服务
Console.WriteLine(“应用程序”,“调用StartService”);
Android.App.Application.Context.StartService(新意图(Android.App.Application.Context,typeof(LocationService));
//绑定我们的服务(Android会按类型查找正在运行的服务,并放置一个引用)
//在该服务的活页夹上)
//意图告诉操作系统在哪里找到我们的服务(上下文)和服务类型
//我们正在寻找(定位服务)
意向locationServiceIntent=新意向(Android.App.Application.Context,typeof(LocationService));
Console.WriteLine(“应用程序”,“调用服务绑定”);
//最后,我们可以使用我们的意图和服务连接绑定到服务
//在上一步中创建。
Android.App.Application.Context.BindService(locationServiceIntent、locationServiceConnection、Bind.AutoCreate);
}).Start();
}
公共静态无效StopLocationService()
{
//如果StartLocationService任务尚未完成,请检查空值。
Console.WriteLine(“应用程序”、“StopLocationService”);
//解除与LocationService的绑定;否则,StopSelf(以下)将不起作用:
if(locationServiceConnection!=null)
{
Console.WriteLine(“应用程序”,“从LocationService解除绑定”);
Android.App.Application.Context.UnbindService(locationServiceConnection);
}
//停止定位服务:
if(Current.LocationService!=null)
{
Console.WriteLine(“应用程序”,“停止定位服务”);
Current.LocationService.StopSelf();
}
}
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="9" android:versionName="0.1" package="com.divcodelivery.divco">
<uses-sdk android:minSdkVersion="15" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_SURFACE_FLINGER" />
<application android:label="Divco" android:icon="@drawable/logo_size_icon"></application>
</manifest>
以下是我尝试构建手机时的结果:
am开始-n“com.divcodelivery.divco/md58047ec786dcfdff107cc37976c308625.main活动”
开始:Intent{cmp=com.divcodelivery.divco/md58047ec786dcfdff107cc37976c308625.MainActivity}
错误类型3
错误:活动类{com.divcodelivery.divco/md58047ec786dcfdff107cc37976c308625.MainActivity}不存在
无法启动应用程序:设备找不到名为com.divcodelivery.divco/md58047ec786dcfdff107cc37976c308625.MainActivity的组件
从设备/仿真器中删除apk(
adb uninstall com.divcodelivery.divco
或通过设备的设置),清除并构建所有(以便Xamarin将重新安装完整的应用程序),并启动新的调试会话。@sushingover我已将其卸载并在之前完成了此操作。。。但在通过adb卸载、清理和构建(也许还有您的良好氛围)之后,它成功了!非常感谢你!它定期在Visual Studio 2015上执行此操作。在VS2012上从来没有任何问题。