英文:
Unable to call activity for result?
问题
目标:
从另一个活动(对话框)调用方法,并返回用户选择的结果
注意:
我已阅读尽可能多的关于切换到其他活动的文章,以及关于此错误的Stack Overflow帖子。我已尝试了大约8个解决方案,但目前我卡在这一点。
问题:
我遇到了以下错误:
我查看了清单,但它没有列出项目的任何活动,所以我不确定为什么会有这样的建议。我调用的活动在CSPROJ文件中。
我也不确定显式活动类的十六进制部分是什么。
在我尝试以下方式时,我得到了相同的错误:
Intent intent;
if (item is Ph)
{
intent = new Intent(this, typeof(CveActivity));
intent.PutExtraMItem(item);
StartActivityForResult(intent, PH_REQUEST_CODE);
}
我尝试过的:
- StartActivity
- StartActivityForResult
- 调用该活动的方法
- 实例化对象并调用该活动/类的方法
- 检查在csproj文件中确实存在这一点
英文:
Goal:
Call method from another activity (dialog) and return result the user chooses
Note: Ive read as many articles as I can on going to other activities, as well as SO posts on this error. I have tried the solutions from about 8 of them and I am just stuck at this point.
Problem:
I am getting the following error:
Ive looked at the manifest, but it doesnt have any of the projects activities so Im not sure why thats even a suggestion. The activity I am calling IS in the CSPROJ file.
I'm also not sure what the HEX part is of the explicit activity class
I get the same error when I try it this way:
Intent intent;
if (item is Ph)
{
intent = new Intent(this, typeof(CveActivity));
intent.PutExtraMItem(item);
StartActivityForResult(intent, PH_REQUEST_CODE);
}
What Ive tried:
- StartActivity
- StartActivityForResult
- Calling the method of that
activity - Instantiating an object and calling the method of that
activity/class - Checked that this does exist in the csproj file
答案1
得分: 1
根据错误日志,您可以尝试在活动中添加“Activity”标签(例如“CveActivity”)。
[Activity(Label = "CveActivity")]
public class CveActivity : Activity
{
}
我创建了一个演示并实现了这个功能,您可以参考以下代码:
MainActivity.cs
[Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
Button button;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
SetContentView(Resource.Layout.activity_main);
button = FindViewById<Button>(Resource.Id.button1);
button.Click += Button_Click;
}
private void Button_Click(object sender, System.EventArgs e)
{
Intent intent = new Intent(this, typeof(SecondActivity));
StartActivityForResult(intent, 100);
}
protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
if (requestCode == 100)
{
if (resultCode == Result.Ok)
{
Toast.MakeText(this, "result is: " + data.GetStringExtra("Test"), ToastLength.Long).Show();
}
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button1"
android:text="test"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
SecondActivity.cs
[Activity(Label = "SecondActivity")]
public class SecondActivity : Activity
{
Button button;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.second_layout);
button = FindViewById<Button>(Resource.Id.button1);
button.Click += Button_Click;
}
private void Button_Click(object sender, EventArgs e)
{
var intent = new Intent();
intent.PutExtra("Test", "test");
SetResult(Result.Ok, intent);
Finish();
}
}
second_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical">
<Button
android:id="@+id/button1"
android:text="return data"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
英文:
Based on the error log, you can try to add Activity
tag to your activities(e.g. CveActivity
).
[Activity(Label = "CveActivity")]
public class CveActivity : Activity{
}
I created a demo and achieved this function, you can refer to the following code:
MainActivity.cs
[Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
Button button;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);
button = FindViewById<Button>(Resource.Id.button1);
button.Click += Button_Click;
}
private void Button_Click(object sender, System.EventArgs e)
{
Intent intent = new Intent(this, typeof(SecondActivity));
StartActivityForResult(intent, 100);
}
protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
if (requestCode == 100) {
if (resultCode == Result.Ok)
{
Toast.MakeText(this,"result is: " + data.GetStringExtra("Test"),ToastLength.Long).Show();
}
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button1"
android:text="test"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
SecondActivity.cs
[Activity(Label = "SecondActivity")]
public class SecondActivity : Activity
{
Button button;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Create your application here
SetContentView(Resource.Layout.second_layout);
button = FindViewById<Button>(Resource.Id.button1);
button.Click += Button_Click; ;
}
private void Button_Click(object sender, EventArgs e)
{
var intent = new Intent();
intent.PutExtra("Test","test");
SetResult(Result.Ok,intent);
Finish();
}
}
second_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical">
<Button
android:id="@+id/button1"
android:text="return data"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
答案2
得分: 0
StartActivityForResult()已被弃用。尝试使用registerForActivityResult()。查看以下链接获取更多信息https://developer.android.com/training/basics/intents/result。可能会有帮助。
英文:
StartActivityForResult() is deprecated. Try using registerForActivityResult(). Check the below link for more info
https://developer.android.com/training/basics/intents/result. It might help.
答案3
得分: 0
由于您要启动的活动是您项目的活动,您应在AndroidManifest.xml文件中(在application
节点内)注册它:
<application ...>
<activity android:name="your.package.YourActivity"
android:exported="true" />
<!-- 所有其他组件 -->
</application>
英文:
As activity you want to launch is your project's activity, you should register it in the AndroidManifest.xml file (inside application
node):
<application ...>
<activity android:name="your.package.YourActivity"
android:exported="true" />
<!-- all other components -->
</application>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论