无法调用活动以获取结果?

huangapple go评论65阅读模式
英文:

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 = &quot;CveActivity&quot;)] 
    public class CveActivity : Activity{

     }

I created a demo and achieved this function, you can refer to the following code:

MainActivity.cs

[Activity(Label = &quot;@string/app_name&quot;, Theme = &quot;@style/AppTheme&quot;, 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 &quot;main&quot; layout resource
        SetContentView(Resource.Layout.activity_main);

        button = FindViewById&lt;Button&gt;(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,&quot;result is: &quot; + data.GetStringExtra(&quot;Test&quot;),ToastLength.Long).Show();
            }
        }
    }
}

activity_main.xml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt; 
&lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:orientation=&quot;vertical&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;&gt;

    &lt;Button
        android:id=&quot;@+id/button1&quot;
        android:text=&quot;test&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;wrap_content&quot;/&gt;
&lt;/LinearLayout&gt;

SecondActivity.cs

[Activity(Label = &quot;SecondActivity&quot;)] 
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&lt;Button&gt;(Resource.Id.button1);
        button.Click += Button_Click; ;
    }

    private void Button_Click(object sender, EventArgs e)
    {
        var intent = new Intent();
        intent.PutExtra(&quot;Test&quot;,&quot;test&quot;);
        SetResult(Result.Ok,intent);
        Finish();
    }
}

second_layout.xml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt; 
&lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    android:orientation=&quot;vertical&quot;&gt;
        &lt;Button
        android:id=&quot;@+id/button1&quot;
        android:text=&quot;return data&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;wrap_content&quot;/&gt;

&lt;/LinearLayout&gt;

答案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):

&lt;application ...&gt;
  &lt;activity android:name=&quot;your.package.YourActivity&quot; 
          android:exported=&quot;true&quot; /&gt;
  &lt;!-- all other components --&gt;
&lt;/application&gt;

huangapple
  • 本文由 发表于 2023年4月20日 03:41:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76058249.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定