【问题标题】:Monodevelop row selected on tableviewsource causes null reference exception在 tableviewsource 上选择的 Monodevelop 行导致空引用异常
【发布时间】:2013-02-14 19:18:02
【问题描述】:
public delegate bool SelectedValueForDropDown (string name);
public partial class BasicTableViewSource : UITableViewSource
{
public override void RowSelected(UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath)
    {
        SelectedValueForDropDown selValue;
        // Do something for the selected row
        string itemSel = this._tableItems[indexPath.Section];
        Console.WriteLine("90 the item selected is :{0}",itemSel);

        DetailViewController dvc = new DetailViewController();
        selValue = dvc.SelectedValueForPosition;
        //bool val = dvc.SelectedValueForPosition(itemSel);
        ValueSelectedDD valDD = new ValueSelectedDD();

        bool success = valDD.HandleValueSelected(selValue);
        Console.WriteLine("100 the value is : {0}",success);
    }
.....
} //class closed

public class ValueSelectedDD
{
    public bool HandleValueSelected (SelectedValueForDropDown selValue)
    {
        bool success =false;
        success = selValue("CEO");

        return success;
    }
}
//This method is in DetailViewController.designer.cs class.
public bool SelectedValueForPosition (string name)
    {
        Console.WriteLine("the value selected is :{0}",name);
        btnPosition.SetTitle(name,UIControlState.Normal); //Exception occurs here
        return true;
    }

我正在处理简单的 MonoDevelop 项目。所以我有一个表格视图作为我的按钮的弹出窗口。我点击它,我有 3 个选项。我选择一个,按钮的标签应该被选择的选项替换。选定的行在 tableviewsource 类中。 DetailViewController 是我可以单击实际按钮的类。我得到了一个例外

System.NullReferenceException: Object reference not set to an instance of an object

 at EmployeeWithDropDown.DetailViewController.SelectedValueForPosition (System.String name) [0x0000b] in /Users/.../EmployeeWithDropDown/EmployeeWithDropDown/DetailViewController.designer.cs:106

 at EmployeeWithDropDown.ValueSelectedDD.HandleValueSelected (EmployeeWithDropDown.SelectedValueForDropDown selValue) [0x00002] in /Users/.../EmployeeWithDropDown/EmployeeWithDropDown/BasicTableViewSource.cs:109
   at EmployeeWithDropDown.BasicTableViewSource.RowSelected (MonoTouch.UIKit.UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath) [0x00036] in /Users/.../EmployeeWithDropDown/EmployeeWithDropDown/BasicTableViewSource.cs:99

 at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
  at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0004c] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:38
  at EmployeeWithDropDown.Application.Main (System.String[] args) [0x00000] in /Users/.../EmployeeWithDropDown/EmployeeWithDropDown/Main.cs:17

System.NullReferenceException has been thrown.
Object Reference not set to an instance of an object.

如果您需要更多信息,请询问。谢谢。

编辑:

public partial class PopoverContentViewcontroller : UIViewController
{
    public override void ViewDidLoad ()
    {

        base.ViewDidLoad ();
        table = new UITableView(View.Bounds);
        table.AutoresizingMask = UIViewAutoresizing.All;
        string[] tableItems = new string[]{"CEO","Developer","IT"};
        //List<String> wordList = Arrays.asList(words); 
        List<string> lst = new List<string>(tableItems);

        table.Source = new BasicTableViewSource(lst);
        Add (table);
       }
 .....
}

这是填充表格以进行下拉的弹出控制器。即这里的值被填充在下拉列表中..

编辑2:

                    DetailViewController dvc;
                    .....
            table.Source = new BasicTableViewSource(lst,dvc);
        Add (table);

【问题讨论】:

  • 这很令人困惑。 DetailViewController 是 tableview 的父级还是子级(谁调用谁?)。在 RowSelected you() 中创建一个新的 DVC 实例,但不显示它。如果 btnPosition 是 DVC 上的 UI 元素,如果 DVC 尚未呈现,则它可能不存在(即,它为 NULL)。
  • @Jason。我添加了一些代码。所以我在 DetailViewController 上有 btnPosition。单击它会从 Popoverviewcontentcontroller 填充一个 uitableviewcontroller。然后选择一行。在 basictableviewsource 中选择的行被调用。例如,我得到值“CEO”。然后我的意图是从 Rowselected 启动一个委托到 DetailViewController 类,然后将选定的值作为按钮上的标签。这有意义吗?

标签: ios uitableview drop-down-menu xamarin.ios uipopovercontroller


【解决方案1】:

您正在 RowSelected 方法中创建 DetailViewController 的新实例。您需要的是对已经存在的 DetailViewController 的引用。

创建 TableViewController 时,需要传入对“父级”DetailViewController 的引用。这样,您可以在 RowSelected 方法中调用现有 DVC 上的方法。

【讨论】:

  • 我在代码中按名称 EDIT2 进行了新更改。我将 DetailViewController 的引用传递给了我的 BasicTableViewSource,但仍然显示异常。我在 RowSelected 中使用了该引用,我得到了这个异常: NSInvalidArgumentException 原因: - [DetailViewController btnPositionClick:]:unrecognized selector sent to instance。你能解释一下或者指出我哪里出错了。
  • 如果不看更多代码,我想我无法回答这个问题。我建议将此移至 Xamarin 论坛,它更适合进行扩展讨论。
  • 我昨天自己做了。但那里也没有回复。
  • 您发布的问题基本上只是指向 SO 的链接。我建议你发布你正在处理的两个控制器的完整源代码,这样我们就可以看到你正在做的事情的上下文。
  • 我在那里发布了完整项目的链接。可以看看吗?
猜你喜欢
  • 2018-12-01
  • 2016-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多