Android Javaでnullpointエラーが発生する原因とは?

このQ&Aのポイント
  • Android Javaでnullpointエラーが発生する原因について教えてください。
  • TouchOnListenerの部分でエラーが発生しているようですが、具体的な原因を教えていただけますか?
  • また、設定したstringが適切であるかについても確認していただきたいです。
回答を見る
  • ベストアンサー

android javaについて質問です

android javaについて質問です public class MailMotion extends Activity implements OnTouchListener, OnClickListener { private FrameLayout frameLayout01; private ImageView target; private Button trash; private int targetLocalX; private int targetLocalY; private int screenX; private int screenY; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.motion); frameLayout01 = (FrameLayout)findViewById(R.id.FrameLayout01); target = (ImageView)findViewById(R.drawable.toudai); target.setOnTouchListener(this); trash = (Button)findViewById(R.id.trash); trash.setOnClickListener(this); } @Override public boolean onTouch(View v, MotionEvent event) { int x = (int)event.getRawX(); int y = (int)event.getRawY(); switch(event.getAction()) { case MotionEvent.ACTION_DOWN: targetLocalX = target.getLeft(); targetLocalY = target.getTop(); screenX = x; screenY = y; break; case MotionEvent.ACTION_MOVE: int diffX = screenX - x; int diffY = screenY - y; targetLocalX -= diffX; targetLocalY -= diffY; target.layout(targetLocalX, targetLocalY, targetLocalX + target.getWidth(); targetLocalY + target.getHeight()); screenX = x; screenY = y; break; case MotionEvent.ACTION_UP: int trashLeft = trash.getLeft() + trash.getWidth()/2; int trashTop = trash.getTop() + trash.getHeight()/2; int targetRight = target.getLeft() + target.getWidth(); int targetBottom = target.getTop() + target.getHeight(); if (targetRight > trashLeft && targetBottom > trashTop) { frameLayout01.removeView(target); } break; } return true; } @Override public void onClick(View v) { int childCount = frameLayout01.getChildCount(); if(childCount == 1) { frameLayout01.addView(target); }}} 上記のものを起動させると、nullpointが出てきます 前回の質問をもとに、デバッグをしてみたのですが、touchonLisnerのところでエラーが起きました 勉強不足なので原因がよくわかっておりません 間違いを教えていただけないでしょうか? stringは <drawable name="custom_button">#ffff00</drawable> で設定して、おそらく大丈夫だと思います <?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/FrameLayout01" android:layout_height="match_parent" android:layout_width="match_parent" android:layout_weight="10" > <Button android:layout_gravity="bottom|right" android:layout_width="30dp" android:layout_height="30dp" android:id="@+id/trash" android:background="@drawable/custom_button"/> <ImageView android:id="@+id/ImageView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="@string/aaaa" android:src="@drawable/aaa" android:layout_gravity="center" /> </FrameLayout>

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

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

  • ベストアンサー
  • teketon
  • ベストアンサー率65% (141/215)
回答No.1

パッと見た感じ、R.drawable.toudaiがXMLに定義されていません。 > target = (ImageView)findViewById(R.drawable.toudai); そのため、上記でtagetにはnullが設定されているのではないでしょうか。 XMLの定義と、プログラムの対応箇所について、確認されたほうが良いです。

関連するQ&A

  • androidのアプリを作ってるんですが

    import android.os.Bundle; import android.app.Activity; import android.util.Log; import android.view.Menu; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; import android.widget.TextView; import android.content.Intent; import android.widget.ImageView; public class GameActivity extends Activity implements OnTouchListener{ private ImageView target; @Override protected void onCreate(Bundle savedInstanceState) { target = (ImageView)findViewById(R.id.imageView1); super.onCreate(savedInstanceState); setContentView(R.layout.activity_game); target.setOnTouchListener(this); } @Override public boolean onTouch(View v, MotionEvent motion) { switch(motion.getAction()) { case MotionEvent.ACTION_DOWN: break; case MotionEvent.ACTION_UP: break; } return true; } } これからゲーム本体を作っていこうとしてるんですが、onTouchメソッドを実装してから実機で動作しなくなり、先に進めず困っています。なぜこれで動作しないのか私の知識不足でよくわかりません。 回答お願いいたします。

    • ベストアンサー
    • Java
  • Android開発についての初心者質問です。

    Android開発についての質問です。初心者でAndroidアプリを作っています。 初心者でAndroidアプリを作っています。 XMLからの実装をしたいのですが、 やり方がよく分かりません。 どなたかご教授頂けないでしょうか? よろしくお願いいたします。 やりたいことは、imageButtonをクリックしたら imageViewの画像部分が変わる。 imageButton1をクリック → imageView1が表示 imageButton2をクリック → imageView2が表示 デフォルトでは、imageView1が表示している状態 buttonのリスナーを登録するのが関の山で、ボタンクリック時の imageView表示切替処理等ちんぷんかんぷんです。 XMLはこういった感じで書いてます。 よろしくお願いいたします。 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content"> <FrameLayout android:id="@+id/frameLayout1" android:layout_height="wrap_content" android:foregroundGravity="center" android:layout_width="match_parent" > <ImageView android:layout_height="wrap_content" android:src="@drawable/icon" android:id="@+id/imageView1" android:layout_gravity="center" android:layout_width="match_parent" > </ImageView> </FrameLayout> <LinearLayout android:layout_height="wrap_content" android:id="@+id/linearLayout1" android:layout_width="match_parent" > <ImageButton android:id="@+id/imageButton1" android:layout_height="wrap_content" android:src="@drawable/icon" android:layout_width="wrap_content" > </ImageButton> <ImageButton android:id="@+id/imageButton2" android:layout_width="wrap_content" android:src="@drawable/icon" android:layout_height="wrap_content" > </ImageButton> <ImageButton android:id="@+id/imageButton3" android:layout_width="wrap_content" android:src="@drawable/icon" android:layout_height="wrap_content" > </ImageButton> </LinearLayout> </LinearLayout>

  • Android JAVAへでの質問です

    (←※この部分!) の意味がわからないのでどなたか教えていただけないでしょうか? private EditText edit1; ですが、なぜ private がついているのでしょうか?なくても動きましたが何か意味があるのでしょうか? そしてどうして、この子はここにいるのでしょうか? Button btn1 と一緒に仲良くいてもいいんじゃないでしょうか? ================ import android.app.Activity; import android.os.Bundle; import android.text.Editable; import android.view.*; import android.view.View.OnClickListener; import android.widget.*; public class Test2Activity extends Activity { private EditText edit1;(←※この部分!) @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); edit1 = (EditText)this.findViewById(R.id.EditText1); Button btn1 = (Button)this.findViewById(R.id.button1); btn1.setOnClickListener(new MyClickA()); } class MyClickA implements OnClickListener { public void onClick(View view){ Editable e = edit1.getText(); int n = Integer.parseInt(e.toString()); Toast toast = Toast.makeText(getApplicationContext(), "数値=" + n, Toast.LENGTH_SHORT); toast.show(); } } }

    • ベストアンサー
    • Java
  • Androidの画面表示のタイミングについて

    androidで、画面のタッチイベントを感知し、画像やボタンを表示させたいと考えています。 その際、ボタンの表示を遅らせるため、sleep()を入れて、表示タイミングをずらそうとしましたが、うまくいかず、ボタンが遅れて表示されるのではなく、画面全体がsleep()で指定した時間分遅れて表示されます。 ファンクションの終了時に画面の表示がまとめて行われるようですが、これはJAVAの仕様なのでしょうか? //タッチイベント public boolean onTouchEvent(MotionEvent event){ //背景画像の表示 final LinearLayout layout= (LinearLayout)findViewById(R.id.Layout); layout.setBackgroundResource(R.drawable.gazou1); //ボタンの表示を遅らせるためスリープ try{ Thread.sleep(3000); }catch (InterruptedException e) { System.out.println(e); } //ボタンの表示 Button a = (Button)findViewById(R.id.button_id); a.setVisibility(View.VISIBLE); return true; } ようろしくお願いいたします。

    • ベストアンサー
    • Java
  • Androidの画面レイアウトがくずれます

    同じサイズの画像を並べても、シミュレータで表示するとばらばらのサイズになってしまいます。 もう1ヶ月以上解決できなくて困っています。 写真はXMLベースで4つの画像を表示した結果です。 シミュレータでやってもほぼ同じ結果です。 元々の画像では、茶色と緑が同じ幅で、ピンクと青が同じ幅です。4つの画像とも高さは一緒です。 サイズの変更等はしてません。 GraphicalLayoutoは5.1inWVGA、シミュレータはWVGA854、Android2.3.3です ソースです。 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content"> <ImageView android:id="@+id/left" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/left"/> <LinearLayout android:id="@+id/center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@+id/left" android:orientation="vertical"> <ImageView android:id="@+id/top" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/top"/> <ImageView android:id="@+id/bottom" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/bottom"/> </LinearLayout> <ImageView android:id="@+id/right" android:layout_height="wrap_content" android:src="@drawable/right" android:layout_toRightOf="@+id/center"/> </RelativeLayout>

  • Androidプログラムについて

    多数の画像を、パラパラ漫画のように切り替えていく方法について相談があり、投稿させていただきました。 以前、xmlファイルで画像を読み込む方法が簡単だということで、hirotn様にご回答いただき、 動作させることができました。 ありがとうございました。 携帯の実機にインストールし、エラーも出ず動作することも確認できたのですが、 画像を切り替えるタイミングがあまりにも遅くなっており困っています。 画像が多い場合は、メモリ不足等起こっているのでしょうか。 プログラムで解決できる方法があれば教えていただきたく、よろしくお願いします。 (ちなみに、xmlファイルで読み出すファイルは100枚程度(80MB程度)です。 もっと数を増やしたり、画質をよくしたいとも思っております。) ※サンプルは下記で見つけました。 http://monoist.atmarkit.co.jp/mn/articles/1205/21/news003.html (以下、プログラム) package jp.test.animation.frame; import android.app.Activity; import android.content.Context; import android.graphics.drawable.AnimationDrawable; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ImageView; public class FrameAnimationTestActivity extends Activity { // ボタン Button mBtnAnimation; Button m5BtnAnimFromXML; // ビュー ImageView mImageAnimation; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mBtnAnimation = (Button) findViewById( R.id.button_animation ); m5BtnAnimFromXML = (Button) findViewById( R.id.button_anim_from_xml ); mImageAnimation = (ImageView) findViewById( R.id.image_animation ); mBtnAnimation.setOnClickListener(mClickListener); m5BtnAnimFromXML.setOnClickListener(mClickListener); } View.OnClickListener mClickListener = new View.OnClickListener() { public void onClick(View v) { // アニメーション中なら、停止 Drawable d = mImageAnimation.getBackground(); if( d != null ){ try{ if( ((AnimationDrawable) d).isRunning() ){ ((AnimationDrawable) d).stop(); return; } } catch( RuntimeException e ){ e.printStackTrace(); } } //if( v == mBtnAnimation ){ //frameAnimationTest( //FrameAnimationTestActivity.this, mImageAnimation ); //} if( v == m5BtnAnimFromXML ){ frameAnimationFromXMLTest( mImageAnimation ); } } }; // フレームアニメーションを XML から読み込む void frameAnimationFromXMLTest( View v ){ // リソースからアニメーションを読み込み、ビューに設定 v.setBackgroundResource( R.drawable.droid_jump ); // ビューからアニメーションを取り出し AnimationDrawable anim = (AnimationDrawable)v.getBackground(); // アニメーション開始 anim.start(); } }

  • Androidプログラミング アプリが停止する

    eclipseでボタンを押すと画像が表示されるアプリをつくろうとしていましたがボタンをを押すとポップアップで「~を停止します。」と表示されアプリが落ちてしまいます。 Ver4.0を使用しています。 package my.application.imageviewer; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.ImageView; public class ImageViewer extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } public void showImage(View view) { ImageView imageView1 = (ImageView)findViewById(R.id.imageView1); imageView1.setImageResource(R.drawable.sample); } } プログラムにミスがあるのでしょうか? 宜しくお願い致します。

  • すいません、Android初心者です

    最近EclipseでAndroidの勉強を始めたものです。 早速ですが、以下のソースを実行すると、何故か「[アプリ名](パッケージ名)が予期せず停止 しました。やり直してください」と出てきます・・・コメントの部分をコメントアウトするとうまく動作 するのですが、何故でしょうか?なお、ターゲットは2.3.3のものを利用しています。 /* +++ Javaソース +++ */ import android.app.Activity; import android.os.Bundle; import android.widget.Button; public class Android_Test01Activity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Button button1 = (Button) findViewById(R.id.button1); //button1.setText("button1"); setContentView(R.layout.main); } } /* +++ XMLソース +++ */ <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" /> </LinearLayout>

    • ベストアンサー
    • Java
  • 【Android開発】ボタンのテキスト表示

    Android初心者です。現在画面遷移先の画面に配列で設定した質問文、選択肢を表示するアプリを制作しています。質問文はTextView、選択肢は2つのButtonにテキストとして表示する。画面遷移した後にsetQuestion()、setChoiceを呼び出したいんですが実機で試したところ、画面遷移した後に「問題が発生しため、(アプリ名)を終了します」とポップアップが出て、アプリが落ちます。 この2つのメソッドをonCreateに入れるのは間違なのでしょうか?どうすればうまくいきますか?初心者なのでうまく書けてないかも知れません。 よろしくお願いします。 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_sub); setChoice(); setQuestion();  } int number = 0; //質問番号 //選択肢 Button button1; Button button2; Button button3; Button button4; //質問文を配列に格納 static String question[] = { "質問文1", "質問文2", }; //選択肢 static String choice[][] = { {"1番", "2番", "3番", "4番"}, {"1番", "2番", "3番", "4番"}, }; //問題の回答 static String answer[] = { "2番", "4番" }; //出力 //質問文を表示 public void setQuestion() { String setQuestion = question[number]; // TextView question = (TextView) findViewById(R.id.Question); question.setText(setQuestion); } //選択肢の表示 public void setChoice() { TextView button1 = (Button) findViewById(R.id.button1); TextView button2 = (Button) findViewById(R.id.button2); button1.setText(choice[number][0]); button2.setText(choice[number][1]); } xml <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="Question" android:id="@+id/Question" android:textSize="40dp" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="81dp" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button1" android:textSize="30dp" android:onClick="choiceClick" android:layout_above="@+id/button2" android:layout_centerHorizontal="true" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button2" android:textSize="30dp" android:onClick="choiceClick" android:layout_alignParentBottom="true" android:layout_alignLeft="@+id/button1" android:layout_alignStart="@+id/button1" />

  • androidアプリ開発 -間違い探しアプリ-

    androidの間違い探しアプリを作ろうと思っています。 イメージ的には背景用のImageViewに画像を表示して、 間違っている箇所にカラのImageViewを載せ、 ImageViewがクリックされたら正解というような仕組みにしようと思っています。 そこで、下記コードを書いています。 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center_vertical" > <ImageView android:id="@+id/mainImage" android:layout_width="fill_parent" android:layout_height="fill_parent" android:src="@drawable/stage1" ></ImageView> <ImageView android:id="@+id/onImage" android:layout_width="40dip" android:layout_height="40dip" android:layout_marginLeft="240dp" android:layout_marginTop="180dp" android:onClick="seikai" /> </RelativeLayout> が、解像度や画面サイズによってカラのImageViewの場所が変わってしまい、うまくいきません。 resフォルダ内に 「layout-land-hdpi」 「layout-land-ldpi」 「layout-land-mdpi」 「layout-land-xhdpi」 フォルダを作り、解像度ごとに位置の微調整は行なっていますが、 画面サイズが異なるとやはりずれてしまいます。 解像度、画面サイズにかかわらず位置を固定する方法はないのでしょうか。 ない場合はやはり画面サイズと解像度すべての条件のxmlを作成する必要がありますか。 また、そもそも 「イメージ的には背景用のImageViewに画像を表示して、間違っている箇所にカラのImageViewを載せる」やり方が間違っているのでしょうか。

専門家に質問してみよう