質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Visual Studio

Microsoft Visual StudioはMicrosoftによる統合開発環境(IDE)です。多種多様なプログラミング言語に対応しています。

Xamarin

Xamarin(ザマリン)は、iPhoneなどのiOSやAndroidで動作し、C# 言語を用いてアプリを開発できるクロスプラットフォーム開発環境です。Xamarin Studioと C# 言語を用いて、 iOS と Android の両方の開発を行うことができます。

Q&A

解決済

3回答

4499閲覧

Resourceに定義がありませんとエラーが起きる

mercurian-teto

総合スコア75

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Visual Studio

Microsoft Visual StudioはMicrosoftによる統合開発環境(IDE)です。多種多様なプログラミング言語に対応しています。

Xamarin

Xamarin(ザマリン)は、iPhoneなどのiOSやAndroidで動作し、C# 言語を用いてアプリを開発できるクロスプラットフォーム開発環境です。Xamarin Studioと C# 言語を用いて、 iOS と Android の両方の開発を行うことができます。

0グッド

0クリップ

投稿2017/12/23 18:54

編集2017/12/24 17:49

xamarin初心者です。C#はそこそこやったことがあります。
このサイトを参考にプログラムを作っていきました。
しかし、上のサイトにあるように
MainActivity.csを記述してからコンパイルしようとすると

C#

1Button btnM = FindViewById<Button>(Resource.Id.btnSayHello); 2TextView txtV = FindViewById<TextView>(Resource.Id.txtMsg);

の箇所に対して

resourceにIDの定義がありません
また
No resource type specified (at 'id' with value '@+txtMsg').
とエラーが発生します。
上のサイトを見てみてもIDの定義を記述するといった手順を踏んでいません。

Resource.Designer.csに何か記述するかと推測されるのですが、
何を書けばいいのかさっぱりわかりません。
どうすればエラーが解けるか回答お願いします。

追記1
MainActivity.csのコードです。

C#

1using Android.App; 2using Android.Widget; 3using Android.OS; 4 5namespace anti_offtime 6{ 7 [Activity(Label = "DroidSample", MainLauncher = true, Icon = "@drawable/icon")] 8 public class MainActivity : Activity 9 { 10 11 protected override void OnCreate(Bundle bundle) 12 { 13 base.OnCreate(bundle); 14 15 // Set our view from the "main" layout resource 16 SetContentView(Resource.Layout.Main); 17 18 Button btnM = FindViewById<Button>(Resource.Id.btnSayHello); 19 TextView txtV = FindViewById<TextView>(Resource.Id.txtMsg); 20 21 btnM.Click += delegate 22 { 23 txtV.Text = "こんにちは"; 24 }; 25 } 26 } 27}

追記2
Main.axml

C#

1<?xml version="1.0" encoding="utf-8"?> 2<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent"> 6 <Button 7 android:text="btnSayHello" 8 android:layout_width="match_parent" 9 android:layout_height="wrap_content" 10 android:id="@+id/btnSayHello" /> 11 <TextView 12 android:textAppearance="?android:attr/textAppearanceLarge" 13 android:layout_width="match_parent" 14 android:layout_height="wrap_content" 15 android:id="@+txtMsg" /> 16 <TextView 17 android:text="Large Text" 18 android:textAppearance="?android:attr/textAppearanceLarge" 19 android:layout_width="match_parent" 20 android:layout_height="wrap_content" 21 android:id="@+id/textMsg" /> 22</LinearLayout>

追記3

上のプログラムがなぜか起動できなくなったので再度作り直しました。
コードは以下に示します。
Main.axml

C#

1<?xml version="1.0" encoding="utf-8"?> 2<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent"> 6 <Button 7 android:text="Button" 8 android:layout_width="match_parent" 9 android:layout_height="wrap_content" 10 android:id="@+id/btnSayHello" /> 11 <TextView 12 android:text="Large Text" 13 android:textAppearance="?android:attr/textAppearanceLarge" 14 android:layout_width="match_parent" 15 android:layout_height="wrap_content" 16 android:id="@+id/txtMsg" /> 17</LinearLayout>

MainActivity.cs

C#

1using Android.App; 2using Android.Widget; 3using Android.OS; 4 5namespace anti_offtime 6{ 7 [Activity(Label = "offtimekiller", MainLauncher = true, Icon = "@drawable/icon")] 8 public class MainActivity : Activity 9 { 10 11 protected override void OnCreate(Bundle bundle) 12 { 13 base.OnCreate(bundle); 14 15 // Set our view from the "main" layout resource 16 SetContentView(Resource.Layout.Main); 17 18 Button btnM = FindViewById<Button>(Resource.Id.btnSayHello); 19 TextView txtV = FindViewById<TextView>(Resource.Id.txtMsg); 20 21 btnM.Click += delegate 22 { 23 txtV.Text = "こんにちは"; 24 }; 25 } 26 } 27}

エラーが

C#

1// Set our view from the "main" layout resource 2 SetContentView(Resource.Layout.Main); 3 4 Button btnM = FindViewById<Button>(Resource.Id.btnSayHello); 5 TextView txtV = FindViewById<TextView>(Resource.Id.txtMsg);

これらのコードに対して現在のコンテキストにresourceという名前は存在しません

追記4再度作り直しました

MainActivity.cs

using Android.App; using Android.Widget; using Android.OS; namespace App5 { [Activity(Label = "DroidSample", MainLauncher = true, Icon = "@drawable/icon")] public class MainActivity : Activity { protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); // Set our view from the "main" layout resource SetContentView(Resource.Layout.Main); Button btnM = FindViewById<Button>(Resource.Id.btnSayHello); TextView txtV = FindViewById<TextView>(Resource.Id.txtMsg); btnM.Click += delegate { txtV.Text = "こんにちは"; }; } } }

Main.axml

C#

1<?xml version="1.0" encoding="utf-8"?> 2<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent"> 6 <Button 7 android:text="Button" 8 android:layout_width="match_parent" 9 android:layout_height="wrap_content" 10 android:id="@+id/btnSayHello" /> 11 <TextView 12 android:text="Large Text" 13 android:textAppearance="?android:attr/textAppearanceLarge" 14 android:layout_width="match_parent" 15 android:layout_height="wrap_content" 16 android:id="@+id/txtMsg" /> 17</LinearLayout>

Resource.Designer.cs

も追記しておきました

C#

1#pragma warning disable 1591 2//------------------------------------------------------------------------------ 3// <auto-generated> 4// このコードはツールによって生成されました。 5// ランタイム バージョン:4.0.30319.42000 6// 7// このファイルへの変更は、以下の状況下で不正な動作の原因になったり、 8// コードが再生成されるときに損失したりします。 9// </auto-generated> 10//------------------------------------------------------------------------------ 11 12[assembly: global::Android.Runtime.ResourceDesignerAttribute("App5.Resource", IsApplication=true)] 13 14namespace App5 15{ 16 17 18 [System.CodeDom.Compiler.GeneratedCodeAttribute("Xamarin.Android.Build.Tasks", "1.0.0.0")] 19 public partial class Resource 20 { 21 22 static Resource() 23 { 24 global::Android.Runtime.ResourceIdManager.UpdateIdValues(); 25 } 26 27 public static void UpdateIdValues() 28 { 29 } 30 31 public partial class Attribute 32 { 33 34 static Attribute() 35 { 36 global::Android.Runtime.ResourceIdManager.UpdateIdValues(); 37 } 38 39 private Attribute() 40 { 41 } 42 } 43 44 public partial class Layout 45 { 46 47 // aapt resource value: 0x7f020000 48 public const int Main = 2130837504; 49 50 static Layout() 51 { 52 global::Android.Runtime.ResourceIdManager.UpdateIdValues(); 53 } 54 55 private Layout() 56 { 57 } 58 } 59 60 public partial class String 61 { 62 63 // aapt resource value: 0x7f030000 64 public const int app_name = 2130903040; 65 66 static String() 67 { 68 global::Android.Runtime.ResourceIdManager.UpdateIdValues(); 69 } 70 71 private String() 72 { 73 } 74 } 75 } 76} 77#pragma warning restore 1591 78

C#

1Button btnM = FindViewById<Button>(Resource.Id.btnSayHello); 2 TextView txtV = FindViewById<TextView>(Resource.Id.txtMsg);

にたいして ResouceにIDの定義がありませんと出てきます

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Zuishin

2017/12/25 03:35

挙げられた URL の冒頭ではプロパティウィンドウから Id にアクセスしていますが、これはできるのですか?
guest

回答3

0

ベストアンサー

こんにちは。田淵です。

Xamarin の質問をするときには、Xamarin.Forms なのか、Xamarin ネイティブ(Xamarin.iOS/Xamarin.Android)なのか、Xamarin ネイティブの場合は Android なのか iOS なのかを明記するとより的確な回答が得られると思います。

今回は Xamarin.Android のようですね。

FindViewById メソッドを使用するのは、Activity から UI コントロールを使えるようにするためです。
逆に言うと、UI コントロールを定義して、それを使うために FindViewById します。
今回は Resource がない。というエラーなので、「UI コントロールを用意していない」のではないかと推測します。(axml の記述も質問中に出てきていないので)

まず、Resources\layout\Main.axml を開き、Button と TextView を配置して、それぞれに btnSayHellotxtMsg と名前を付けてください。

XML なので直接記述することもできますが、Java や Kotlin での Android ネイティブ開発に慣れていない場合は、デザイナーで配置するのが簡単だと思います。

デザイナーのイメージは例えば Visual Studio 2017 の場合は以下のような感じでコントロールを配置して右下の「プロパティ」ウィンドウの「id」を設定するだけです。

Android デザイナー

XML で直接記述する場合は例えば次のようになるかと思います。

xml

1<!-- ...何らかのレイアウト内... --> 2<Button android:id="@+id/btnSayHello" 3 android:layout_width="match_parent" 4 android:layout_height="wrap_content" 5 android:text="Say Hello" /> 6<TextView android:id="@+id/txtMsg" 7 android:layout_width="match_parent" 8 android:layout_height="wrap_content" 9 android:text="" />

なお、デザイナーまたは XML で画面を作成すると、Resource.Designer.cs は Visual Studio が自動的に再生成してくれますので、中を手動で編集してはいけませんので、ご注意ください。

一助になれば幸いです。

投稿2017/12/24 07:00

編集2017/12/24 07:02
ytabuchi

総合スコア335

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

mercurian-teto

2017/12/24 15:10

回答ありがとうございます。 Resources\layout\Main.axml を開き、Button と TextView を配置して、それぞれに btnSayHello、txtMsg と名前を付けてください。 としたのですが、 追記3のようになってしまいます。
mercurian-teto

2017/12/24 17:52

すいません追記4をご覧ください
ytabuchi

2017/12/25 00:12

Resource.Designer.cs にまだコントロールの ID が追加されていないですね。 Resource.Designer.cs はビルド時に自動生成されます。(ビルド前に生成されることもあるみたいですが、ビルド時は確実に生成されます。) Activiry.cs の FindViewById のコードを一度コメントにして、Main.axml にコントロールを追加した時点で一度ビルドすると、Resource.Designer.cs が確実に追加されると思います。その後コメントを解除してビルドすると恐らく大丈夫かと思います。 または、Resource.Designer.cs を手動で編集したり変な感じになっている可能性があるので、Resource.Designer.cs を一度削除して、Visual Studio を再起動して、Activity に FindViewById がない状態でビルドするのも良い回避策のはずです。 一度ビルドが通れば後はそんなに神経質にならなくても大体ビルドは通るはずです。 頑張ってください。
mercurian-teto

2017/12/25 07:18

一からやり直してみたらなぜかコンパイルが通り普通に実行できるようになりました。不完全なままデバグを実行してからもう一度編集を加えデバグしたからかもしれないのですが、とりあえず回答してくださった方々がおっしゃるようにresource.designer.csにコントロールIDが追加されていなかったのが原因でした。お騒がせして申し訳ありませんでした。
mercurian-teto

2017/12/25 07:21

これからのアプリ開発でもResource.Designer.csの自動生成についてのアドバイスも参考にしていただきます。
guest

0

こんにちは。

Main.axml でエラーは発生していないですか?
Resource.Designer.cs クラス内に Idクラスは生成されてますか?

念のためMain.axmlも提示してください。

投稿2017/12/24 06:34

Tak1wa

総合スコア4791

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

mercurian-teto

2017/12/24 15:11

回答ありがとうございます。 追記3に示しました。
Tak1wa

2017/12/24 15:48

IDがないのではなく、Resourceが無いって言われてるのですか? Xamarin.Androidの空のテンプレートを新規作成し、ビルドできるか確認してみては。開発環境がそもそも準備できていない可能性はないですかね。
mercurian-teto

2017/12/24 17:50

もう一度試すとなぜかエラーが変更して追記4のようになっています。 Resource.Designer.cs クラス内に Idクラスが内容ですが、これが原因ですか。
mercurian-teto

2017/12/25 07:19

前のプログラムを見ると、Resource.Designer.cs クラス内に Idクラスは生成されていませんでした。とりあえず、新しく作りなおしたら自己解決しました。お騒がせして申し訳ありませんでした。
guest

0

using を見直してみてください。Resource が別のクラスを指してはいませんか?

投稿2017/12/23 23:07

Zuishin

総合スコア28660

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Zuishin

2017/12/23 23:15 編集

あと Designer.cs という名前のついたファイルは基本的に書き換えてはいけません。デザイナーによって自動的に作成・更新されるものなので上書きされる可能性があります。
mercurian-teto

2017/12/24 04:04

MainActivity.csに using Android.App; using Android.Widget; using Android.OS; とありますが、ここにresourceに関係するusing ディレクティブが足りないということでしょうか。
mercurian-teto

2017/12/25 07:19

前のプログラムを見ると、Resource.Designer.cs クラス内に Idクラスは生成されていませんでした。とりあえず、新しく作りなおしたら自己解決しました。お騒がせして申し訳ありませんでした。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問