2017-07-06 102 views
0

在嘗試上傳圖片時,我在OnActivityResult方法中遇到了2個問題,根據this。我該如何解決它們? enter image description here 類的完整代碼:在OnActivityResult方法中選擇圖庫圖片的問題

public class AddNewTourActivity : Activity 
    { 
     GettingCountry gettingCountry = new GettingCountry(); 
     private static int idOfChosenCountry, IdOfChosenCategory; 
     private static string status, promptMessage; 
     protected override void OnCreate(Bundle savedInstanceState) 
     { 
      base.OnCreate(savedInstanceState); 

      SetContentView(Resource.Layout.AddNewTour); 

      //clearing lists 
      GettingCountry.listOfCountriesRoot.Clear(); 
      GettingCountry.countriesList.Clear(); 

      GettingCategories gc = new GettingCategories(); 
      //Getting list of countries 
      gettingCountry.Fetch(); 

      //clearing listCategoriesRoot 
      GettingCategories.categoriesList.Clear(); 
      //getting categories method 
      gc.GetCategories(); 
      Spinner categories_spinner = FindViewById<Spinner>(Resource.Id.categories_spinner); 
      Spinner countries = FindViewById<Spinner>(Resource.Id.countries); 
      //adapter 
      ArrayAdapter adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, GettingCategories.categoriesList); 
      ArrayAdapter adapterCountries = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, GettingCountry.countriesList); 
      categories_spinner.Adapter = adapter; 
      countries.Adapter = adapterCountries; 
      countries.ItemSelected += Countries_ItemSelected; 
      categories_spinner.ItemSelected += Categories_spinner_ItemSelected; 
      //FindViewById<EditText>(Resource.Id.owner_id).Text = Login.user_id.ToString(); 

      var title = FindViewById<EditText>(Resource.Id.title); 
      var image = FindViewById<ImageView>(Resource.Id.myImageView); 
      var location = FindViewById<EditText>(Resource.Id.location); 
      var description = FindViewById<EditText>(Resource.Id.description); 
      var price = FindViewById<EditText>(Resource.Id.price); 
      var min_capacity = FindViewById<EditText>(Resource.Id.min_capacity); 
      var max_capacity = FindViewById<EditText>(Resource.Id.max_capacity); 
      var duration = FindViewById<EditText>(Resource.Id.duration); 
      var meet_place_address = FindViewById<EditText>(Resource.Id.meet_place_address); 
      var meet_place_city = FindViewById<EditText>(Resource.Id.meet_place_city); 
      var lat = FindViewById<EditText>(Resource.Id.lat); 
      var lng = FindViewById<EditText>(Resource.Id.lng); 

      //uploading image 
      image.Click += Image_Click; 
      //uploading image ENDED 

      FindViewById<Button>(Resource.Id.add_tour_button).Click += delegate 
      { 
       //setting prompt message empty 
       promptMessage = ""; 
       var client = new RestClient("http://api.locopal.com"); 
       var request = new RestRequest("/experience", Method.POST); 

       //CHECKING THE CORRECTNESS OF THE USER'S INTRODUCTION TO ALL FIELDS 
       if (title.Text.Length < 3) 
       { 
        promptMessage += " Title must have at least 3 symbols.\n"; 
       } 
       if (location.Text.Length < 3) 
       { 
        promptMessage += " Location must have at least 3 symbols.\n"; 
       } 
       if (description.Text.Length < 30) 
       { 
        promptMessage += " Description length must be at least 30 symbols.\n"; 
       } 

       //checking if price, capacity, etc... are integer values 
       int res; 
       bool priceIsInt = false; 
       priceIsInt = Int32.TryParse(price.Text, out res); 

       if (priceIsInt == false) 
       { 
        promptMessage += " Price must be an integer value.\n"; 
       } 

       bool minCapacityIsInt = false; 
       minCapacityIsInt = Int32.TryParse(min_capacity.Text, out res); 
       if (minCapacityIsInt == false) 
       { 
        promptMessage += " Minimum capacity must be an integer value.\n"; 
       } 

       bool maxCapacityIsInt = false; 
       maxCapacityIsInt = Int32.TryParse(max_capacity.Text, out res); 
       if (maxCapacityIsInt == false) 
       { 
        promptMessage += " Maximum capacity must be an integer value.\n"; 
       } 

       bool durationIsInt = false; 
       durationIsInt = Int32.TryParse(duration.Text, out res); 
       if (durationIsInt == false) 
       { 
        promptMessage += " Duration must be an integer value.\n"; 
       } 
       //checking if price, capacity, etc... are integer values ENDED 

       if (meet_place_address.Text.Length < 3) 
       { 
        promptMessage += " Address of meeting place must have at least 3 symbols.\n"; 
       } 

       if (meet_place_city.Text.Length < 3) 
       { 
        promptMessage += " City of meeting place must have at least 3 symbols.\n"; 
       } 

       //checking if lat and lng are doubles 
       double resDouble; 

       bool latIsDouble = false; 
       latIsDouble = Double.TryParse(lat.Text, out resDouble); 
       if (latIsDouble == false) 
       { 
        promptMessage += " Latitude must be a fractional value.\n"; 
       } 

       bool lngIsDouble = false; 
       lngIsDouble = Double.TryParse(lng.Text, out resDouble); 
       if (lngIsDouble == false) 
       { 
        promptMessage += " Longitude must be a fractional value."; 
       } 
       //checking if lat and lng are doubles ENDED 

       //CHECKING THE CORRECTNESS OF THE USER'S INTRODUCTION TO ALL FIELDS ENDED 

       request.AddParameter("api_token", Login.token); 
       request.AddParameter("title", title.Text); 
       request.AddParameter("location", location.Text); 
       request.AddParameter("description", description.Text); 
       request.AddParameter("price", price.Text); 
       request.AddParameter("owner_id", Login.user_id); 
       request.AddParameter("min_capacity", min_capacity.Text); 
       request.AddParameter("max_capacity", max_capacity.Text); 
       request.AddParameter("duration", duration.Text); 
       request.AddParameter("duration_type", 1); 
       request.AddParameter("meet_place_address", meet_place_address.Text); 
       request.AddParameter("meet_place_city", meet_place_city.Text); 
       request.AddParameter("meet_place_country", idOfChosenCountry); 
       request.AddParameter("category_list[0]", IdOfChosenCategory); 
       request.AddParameter("lat", lat.Text); 
       request.AddParameter("lng", lng.Text); 

       try 
       { 
        IRestResponse response = client.Execute(request); 
        var content = response.Content; 
        var myContent = JObject.Parse(content); 
        status = myContent["status"].ToString(); 
       } 
       catch { } 

       if (status == "success") 
       { 
        Toast.MakeText(this, "Tour added successfully", ToastLength.Short).Show(); 
        //setting status variable to null to prevent issues with adding new places in future 
        status = null; 
        StartActivity(typeof(MainActivity)); 
       } 
       else 
       { 
        Toast.MakeText(this, promptMessage, ToastLength.Long).Show(); 
       } 
      }; 
     } 

     private void Image_Click(object sender, EventArgs e) 
     { 
      var imageIntent = new Intent(); 
      imageIntent.SetType("image/*"); 
      imageIntent.SetAction(Intent.ActionGetContent); 
      StartActivityForResult(
       Intent.CreateChooser(imageIntent, "Select photo"), 0); 
     } 

     private void Categories_spinner_ItemSelected(object sender, AdapterView.ItemSelectedEventArgs e) 
     { 
      IdOfChosenCategory = e.Position; 
     } 

     private void Countries_ItemSelected(object sender, AdapterView.ItemSelectedEventArgs e) 
     { 
      idOfChosenCountry = gettingCountry.retrievingChoosenCountryId(GettingCountry.countriesList[e.Position].ToString()); 
     } 

     protected override void OnActivityResult(int requestCode, Result resultCode, Intent data) 
     { 
      base.OnActivityResult(requestCode, resultCode, data); 

      if (resultCode == Result.Ok) 
      { 

       var imageView = FindViewById<ImageView>(Resource.Id.myImageView); 
       imageView.SetImageURI(data.Data); 
      } 
     } 
    } 

回答

0

我改變了命名空間從RecyclerViewSampleRecyclerViewSampl。我只刪除了一封信。然後,我添加完整路徑RecyclerViewSample.Resource.Id.min_capacity作爲以前名稱空間的所有元素的示例。 例如: SetContentView(RecyclerViewSample.Resource.Layout.AddNewTour); 在另一個類中,我對這個新的命名空間做了類比引用。例如: StartActivity(new Intent(this, typeof(RecyclerViewSampl.AddNewTourActivity)));