2017-03-04 119 views
0

我已經在Xamarin中實現了一個項目。在模擬器和設備上運行它時,它顯示不幸的是,應用程序已停止。Xamarin應用程序已停止

我是xamarin的新手,我對這個問題一無所知。 我已經解決了所有的錯誤和警告,但它只顯示那樣。

我的設備是目錄下載如下: enter image description here

定製spinner.cs

namespace App1.Assets.Ccode.Cspinner 
{ 


    class CustomSpinner : BaseAdapter 
    { 
     private Context c; 
     private JavaList<CountryName> countries; 
     private LayoutInflater inflater; 

     public CustomSpinner(Context c, JavaList<CountryName> countries) 
     { 
      this.c = c; 
      this.countries = countries; 
     } 

     public override Java.Lang.Object GetItem(int position) 
     { 
      return countries.Get(position); 
     } 

     public override long GetItemId(int position) 
     { 
      return position; 
     } 

     public override View GetView(int position, View convertView, ViewGroup parent) 
     { 
      if (inflater == null) 
      { 
       inflater = (LayoutInflater)c.GetSystemService(Context.LayoutInflaterService); 
      } 

      if (convertView == null) 
      { 
       convertView = inflater.Inflate(Resource.Layout.country_list, parent, false); 
      } 

      TextView nameTxt = convertView.FindViewById<TextView>(Resource.Id.nameTxt); 
      ImageView img = convertView.FindViewById<ImageView>(Resource.Id.India); 

      //BIND 
      nameTxt.Text = countries[position].Name; 
      img.SetImageResource(countries[position].Image); 


      return convertView; 
     } 

     public override int Count 
     { 
      get 
      { 
       return countries.Size(); 
      } 
     } 

    } 
    } 
    } 

主要activiy

[Activity(Label = "App1", MainLauncher = true, Icon = "@drawable/icon")] 
public class MainActivity : Activity 
{ 
    private Spinner sp; 
    private CustomSpinner adapter; 
    private JavaList<CountryName> countries; 

    protected override void OnCreate(Bundle bundle) 
    { 
     base.OnCreate(bundle); 

     // Set our view from the "main" layout resource 
     SetContentView(Resource.Layout.Main); 

     sp = FindViewById<Spinner>(Resource.Id.country); 
     countries = CountriesList.GetCountrys(); 
     adapter = new CustomSpinner(this, countries); 

     sp.Adapter = (ISpinnerAdapter)adapter; 
     sp.ItemSelected += sp_ItemSelected; 
    } 

    void sp_ItemSelected(object sender, AdapterView.ItemSelectedEventArgs e) 
    { 
     Toast.MakeText(this, countries[e.Position].Name, ToastLength.Short).Show(); 
    } 
} 
} 

清單文件

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="App1.App1" android:versionCode="1" android:versionName="1.0" 
android:installLocation="auto"> 
<uses-sdk android:minSdkVersion="16" /> 
<uses-permission android:name="android.permission.INTERNET" /> 
<application android:label="App1"></application> 
</manifest> 

請保存我的一天,並幫助我在設備上運行它。

謝謝

+0

什麼是目標框架? –

+0

@Mohammed Dastagir你有**還沒有更新**有關'文件'的任何事情 – Mrinmoy

+0

@Mrinmoy對不起,延遲。 –

回答

0

你得到一個NullPointerException異常

file.getAbsolutePath(); 

error是說file objecy是

這是確切的錯誤: - click to see image

+0

請告訴我如何解決它。 –

+0

如何在沒有代碼的情況下幫助親愛的人。該文件對象爲空,您嘗試使用getAbsolutePath()。所以檢查爲什麼文件沒有被初始化 – Mrinmoy

0

隨着您的問題或沒有實際代碼非常少的背景細節,我可以建議你如下:

  1. 編輯您的.csproj。如果文件中有<AndroidUseLatestPlatformSdk>True</AndroidUseLatestPlatformSdk>,請嘗試將其設置爲False,如<AndroidUseLatestPlatformSdk>False</AndroidUseLatestPlatformSdk>。清理一切並重建,重新安裝應用程序。

  2. 轉到AndroidManifest.xml並將'安裝位置'設置爲internalOnly

  3. 嘗試降級Target Framework

P.S:以上是3個不同的建議,而不是3個不同的建議相同的步驟。 上述建議通常有效,但您還沒有提到問題中的大部分細節。