Androidアプリ初心者のためのサンプルクラスの使用方法

このQ&Aのポイント
  • Androidアプリ初心者の方におすすめのサンプルクラスの使用方法を解説します。
  • サンプルクラスを使用する際に発生するエラーについても解説します。
  • より効果的にサンプルクラスを活用するためのヒントもご紹介します。
回答を見る
  • ベストアンサー

Androidアプリプログラミング

Androidアプリ初心者です。 android-sdk-/samples/android-8/ApiDemos/src/com/example/android/apis/graphics/spritetextの中にあるサンプルクラスをつかいたいのですが(本でそのクラスを使ってプログラミングしているので) import com.example.android.apis.graphics.spritetext.*; ↑importしたらエラーになってしまいます。 どうやったらサンプルクラスを使えるのでしょうか? 私の乏しい知識では、限界です…何方様かお知恵をお貸しくださいよろしくお願いします。

  • Java
  • 回答数1
  • ありがとう数1

質問者が選んだベストアンサー

  • ベストアンサー
noname#161640
noname#161640
回答No.1

先の質問にも回答したのですが……、どうも、Androidのプログラミングの前に、まだJavaが分かっていないように思えます。このパッケージにあるクラスを利用したいなら、Eclipseのプロジェクトの「src」フォルダにそのパッケージのソースコードファイルを用意するか、ビルドされたプログラムをライブラリとして追加する必要があります。ここまではわかりますか? 例えば、ソースコードファイル(◯◯.javaという名前のファイルです)があるならば、プロジェクトの「src」フォルダ内に、「com」フォルダ、その中に「excample」フォルダ、その中に「android」フォルダ、その中に「apis」フォルダ、その中に「graphics」フォルダ、その中に「spritetext」フォルダ、というように入れ子の状態でフォルダを用意し、この一番奥にある「spritetext」フォルダの中に「◯◯.java」というファイルが置かれる形となります。 その本でどういう具合に説明してあるのか、あるいはどう利用しているのかわからないのでこれ以上のことはなんともいいようがないのですが……、これでもわからないようであれば、その本でサンプルプロジェクトなどは配布されていませんか? あるいは説明のとおりにやっても動かないようなら、出版社か著者に問い合せてはいかがでしょうか。初心者向けの入門書では、たいてい最初の手順などを細かく説明してあって、その通りにすれば動くようになっているはずですので、それで動かなければ説明が不足しているか間違っているということだろうと思いますので……。

happyflight41
質問者

お礼

good-failure様 ご丁寧に何度もありがとうございます。 やっとエラーがとれ理解もできました! またなにかありましたらお力をおかし下さい 本当にありがとうございました。

関連するQ&A

  • Androidアプリについて

    Androidアプリ初心者のものです。 import com.example.android.apis.graphics.spritetext.*; ↑このinport文を書いたらcom.exampleのパッケージがないとエラーメッセージがでてしまいました。 本を読みながら初期設定をして、プログラムも書いたのですが力及ばず… どなた様か私にお知恵をお貸し下さいヨロシクお願いします。

    • ベストアンサー
    • Java
  • androidプログラミング エラーについて

    現在eclipseでandroidプログラミングに挑戦している超初心者です。 ネットで検索してもなかなかエラーの対処がわかりません。 詳細は以下の通りです。 String[] strArrayStr = getResources().getStringArray(R.array.ary_samples); TextView csTView = (TextView) findViewById(R.id.test); 1行目のarrayとary_samples2行目のtestにエラーが出ます。 R.javaを見ると、クラスやフィールドが自動生成されていなかったので、 public static final class array { public static int ary_samples; } 同じようにtestはidクラスに public static int test; と追記しました そして、実行をすると、先程訂正したR.javaが消えてしまいます。 エラーわかる方、解答お願いいたします。

    • ベストアンサー
    • Java
  • Android Studio Kotlin エラー

    全部のファイルのR.layout.hogehoge_activityのところのRで赤くなってエラーが出ます。 API28 revision 6 emulator-5554 Android 9 Android Studio 3.1.4 SDK tools 26.1.1 言語 Kotlin たくさんアクティビティファイルがありますので、エラー内容がわかるであろう、MainActivityとbuild.gradleと、Manifestファイル、を書きました。 インターネットで調べたのですが、有益な情報が見つかりませんでしたので、詳しい方、お願い済ます。 エラー内容を記します。 FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':app:processDebugResources'. > Failed to process resources, see aapt output above for details. * Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights. * Get more help at https://help.gradle.org BUILD FAILED in 0s 13 actionable tasks: 1 executed, 12 up-to-date 1つめのファイル MainActivity package com.example.yusuke.mysql02 import android.content.Context import android.content.Intent import android.content.SharedPreferences import android.support.v7.app.AppCompatActivity import android.os.Bundle import android.preference.PreferenceManager import android.view.View import android.widget.EditText import com.example.yusuke.mysql02.Grobal.grobalusername import com.example.yusuke.mysql02.R.id.username import android.widget.TextView import android.widget.Toast import com.android.volley.Request import com.android.volley.toolbox.StringRequest import kotlinx.android.synthetic.main.activity_main.* import com.android.volley.Response import com.android.volley.toolbox.JsonObjectRequest import com.android.volley.toolbox.Volley import org.json.JSONObject class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) } val jsonobj = JSONObject() fun nextactivity(v:View){ jsonobj.put("username",username.text) jsonobj.put("password",password.text) val URL = "http://www.example.com/registeractivity.php" val que = Volley.newRequestQueue(this@MainActivity) val req = JsonObjectRequest(Request.Method.POST,URL,jsonobj, Response.Listener{ response -> Grobal.grobalexsist = response.toString() },Response.ErrorListener { Grobal.grobalexsist = "0" }) que.add(req) var nametext : String = username.getText().toString() var passtext : String = password.getText().toString() Grobal.grobalusername = nametext Grobal.grobalpassword = passtext val intent = Intent(this,RegisteredActivity::class.java) startActivity(intent) } fun loginactivity(v: View){ val intent = Intent(this,LoginActivity::class.java) startActivity(intent) } } 2つ目のファイル build.gradle apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' android { compileSdkVersion 28 defaultConfig { applicationId "com.example.yusuke.mysql02" minSdkVersion 20 targetSdkVersion 28 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" implementation 'com.android.support:appcompat-v7:28.0.0-rc02' implementation 'com.android.support.constraint:constraint-layout:1.1.3' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' implementation 'com.android.volley:volley:1.1.1' } 3つ目のファイル Manifestファイル <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.and

  • アンドロイド JAVA 警告

    アンドロイド 初心者です package com.example.test; import android.os.Bundle; import android.app.Activity; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.view.View; public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(new testView(this)); } class testView extends View { public testView(Context context) { super(context); } @Override protected void onDraw(Canvas canvas) { Paint paint = new Paint(); paint.setColor(Color.RED); canvas.drawLine(0, 0, 100, 50, paint); } } } このプラグラムで"Paint paint = new Paint();"のところで "Avoid object allocations during draw/layout operations (preallocate and reuse instead)" の警告が出ます、これを取る方法はありませんか。

    • ベストアンサー
    • Java
  • [android]画面に円が表示されない原因

    こんにちは。 以下のサイトを参考に白い画面に赤い円を表示させたいのですが、実際にはエミュレータ上に表示されません。 logcatのエラーもありません。原因と対処方法をご教授ください。 eclipseのバージョンは以下のとおりです。 Eclipse Platform 3.8.0.v20120607-071945-9gF7jI7nG5qByXMVdkhRMWBQlF4PnDCLybDCPQ http://www.hakkaku.net/articles/20091127-594 ★MainActivity.java package com.example.test001_001; import android.os.Bundle; import android.app.Activity; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.view.Menu; import android.view.View; public class MainActivity extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 描画クラスを設定 setContentView(new TestDrawView(getApplication())); } /* 描画用クラス */ class TestDrawView extends View { public TestDrawView(Context c) { super(c); } } // このメソッドで描画を行う protected void onDraw(Canvas c) { // --------- (*1)Check!! c.drawColor(Color.WHITE); Paint p = new Paint(); p.setColor(Color.RED); p.setStyle(Paint.Style.FILL); c.drawCircle(100, 100, 50, p); } }

  • javahのclasspathが通らない

    (クラスファイルの置き場所) c:\android-ndk-1.5_r1/apps/HelloNDK/bin/com/example/android/hello/HelloNDK.class (javaファイルの置き場所) c:\android-ndk-1.5_r1/apps/HelloNDK/src/com/example/android/hello/HelloNDK.java (javaファイル内のpackageとclass) package com.example.android.ndk.hello; public class HelloNDK extends Activity { (cygwinで入力したコマンド) cd c:\android-ndk-1.5_r1/apps/HelloNDK/bin bash javah com.example.android.hello.HelloNDK (エラーメッセージ) bash: javah: No such file or directory bash javah -jni -classpath com.example.android.hello.HelloNDK などコマンドをいろいろ試しましたが同じエラーでした。 ネットで調べていろいろ試しましたがダメでした。 どのような原因が考えられますか?

  • androidでアプリを作ろうとしてるのですが

    package com.example.test; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.TextView; public class InOutTest extends Activity { /** Called when the activity is first created. */ public TextView txtInfo; public EditText edtText; public Button outputbutton; public TextView txtResult; public TextView txtjudge; public String strInch; public String gettext; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //レイアウト作成 ここから LinearLayout layout = new LinearLayout(this); layout.setOrientation(LinearLayout.VERTICAL); setContentView(layout); //レイアウトを見えるようにする //レイアウト作成 ここまで //GUI部品作成 ここから // ラベル txtInfo = new TextView(this); txtInfo.setText("check"); strInch = ("check"); layout.addView(txtInfo); // エディタ edtText = new EditText(this); layout.addView(edtText); // ボタン txtResult = new Button(this); txtResult.setText("same or difference"); layout.addView(txtResult); // 結果表示用ラベル txtjudge = new TextView(this); txtjudge.setText("check"); layout.addView(txtjudge); //GUI部品作成 ここまで txtResult.setOnClickListener(new OnClickListener(){ public void onClick(View v) { gettext = edtText.toString(); if (strInch==gettext){ txtjudge.setText("ok"); } }; }); }} 上のプログラムで strInchに入ってるデータとgettextで取得した文章を比較して同じなら txtjudgeにOKと引き渡して表示させたいのですがうまく動きません 何方かどうぞよろしくお願い致します

    • ベストアンサー
    • Java
  • [Android]エミュレータで動作しない原因

    こんにちは。java、Android開発初心者です。 「test001」というAndroidプロジェクト、「test001_01」というクラスを作成し、マニフェストを書き換えました。実行すると、再起動して繰り返しても The application has stopped unexpectedly. Please try again. と表示されます。 原因と対処方法を教えていただけませんか。 宜しくお願い致します。 ★test001 Manifest <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.test001" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="16" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.test001.text001_01" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> ★test001_01.java package com.example.test001; import android.os.Bundle; import android.app.Activity; import android.graphics.Color; import android.view.Menu; import android.widget.Button; import android.widget.LinearLayout; import android.widget.LinearLayout.LayoutParams; import android.widget.TextView; public class test001_01 extends Activity { private Button startButton, stopButton; private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.sub); startButton = new Button(this); startButton.setText("Button5"); stopButton = new Button(this); stopButton.setText("Button6"); textView = new TextView(this); textView.setText("TextView1"); textView.setBackgroundColor(Color.YELLOW); LinearLayout.LayoutParams linearLayoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); LinearLayout linearLayout = new LinearLayout(this); linearLayout.setOrientation(LinearLayout.VERTICAL); linearLayout.setLayoutParams(linearLayoutParams); linearLayout.addView(startButton, linearLayoutParams); linearLayout.addView(stopButton, linearLayoutParams); linearLayout.addView(textView, linearLayoutParams); setContentView(linearLayout); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_main, menu); return true; } }

    • ベストアンサー
    • Java
  • Androidプログラミング上達のコツ

    Java及びAndroidプログラミング初心者で オブジェクト指向は始めてですが、基本的概念や言語仕様は理解しております。 今の段階は、簡単なUIを設置したり 本などのサンプルを動かしている段階です。 AndroidのクラスやメソッドなどのAPI日本語のリファレンスはネット上に無いようでしたので そこそこ大きな本屋で探してみましたが、APIリファレンス本はありませんでした。 クラスとメッソッドのイメージを予め掴んでおくと、 コーディングがスムーズに行くと思っています。 英語力の無い私はリファレンスを翻訳するのは大変です。 皆さんはどのようにされているのですか どの程度のこと 例えば、クラス、シグネチャまで覚えておくものなのでしょうか? それとも、 パッケージの概要からどんなクラスがあるかくらいの大雑把な感じで 把握しておき、メッソッドは名前から想像して実際に動作テストして理解するものなのでしょうか。 ある程度慣れると、あのクラスにここれこれのメッソッドがあるんだろうな~ などの感でプログラミングを進められるようになるのですか? イメージが掴める程度で構いません、手がかりになるようなアドバイス頂けると幸いです。

    • ベストアンサー
    • Java
  • androidのopenCVでビルドできません

    初めまして。Androidのプログラミングを学んでいます。 AndroidアプリでopenCVを使いたいと思っています。 まずは環境構築をして、サンプルソースをNDKでビルドして動かそうと思ったのですが どうもサンプルソースなのにCygwin上でビルドをしてもエラーが出てしまいます。 openCVのバージョンはOpenCV-2.3.1です。 NDKのバージョンはandroid-ndk-r8bです。 今回は、AndroidのoepnCVのsamplesフォルダにある”tutorial-4-mixed”をビルドしました。 長すぎるエラー文で、テキストでは載せられないため、画像添付で申し訳ありません。 それでは、ご指南等よろしくお願いいたします。

    • ベストアンサー
    • Java