###発生している問題
invalidate();でonDraw();が再描画されなく、ログ出力で調べたところinvalidate();が効いていないことが判明しました。
###現在の状況
xmlファイルとカスタムビュー(と思われるもの)を扱うファイル、そして操作するためのファイルがあります。xmlファイルでは、
xml
1<?xml version="1.0" encoding="utf-8"?> 2<android.support.percent.PercentFrameLayout 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 xmlns:app="http://schemas.android.com/apk/res-auto" 5 android:id="@+id/pfl" 6 android:layout_width="match_parent" 7 android:layout_height="match_parent" 8 android:gravity="top"> 9 <com.gmail.archerygame.Designgame 10 android:id="@+id/sousa" 11 android:layout_width="wrap_content" 12 android:layout_height="wrap_content" 13 /> 14 <TextView 15 android:id="@+id/powertext" 16 android:layout_width="wrap_content" 17 android:layout_height="wrap_content" 18 android:text="power" 19 android:layout_gravity="left|top" 20 android:textSize="18dp" 21 app:layout_marginLeftPercent="31%" 22 /> 23 <TextView 24 android:id="@+id/staminatext" 25 android:layout_width="wrap_content" 26 android:layout_height="wrap_content" 27 android:text="stamina" 28 android:layout_gravity="left|top" 29 android:textSize="18dp" 30 app:layout_marginTopPercent="6%" 31 app:layout_marginLeftPercent="31%" 32 /> 33 <TextView 34 android:id="@+id/timer" 35 android:layout_width="wrap_content" 36 android:layout_height="wrap_content" 37 android:text="Timer" 38 android:textSize="22dp" 39 android:layout_gravity="right|top" 40 /> 41 42</android.support.percent.PercentFrameLayout>
の、
xml
1<com.gmail.archerygame.Designgame 2 android:id="@+id/sousa" 3 android:layout_width="wrap_content" 4 android:layout_height="wrap_content" 5 />
でカスタムビュー(と思われるもの)をレイアウトしています。
操作するファイルでonTouchイベントである領域がtrueであるときに、カスタムビュー(と思われるもの)を扱うファイルにあるメソッドを呼び出し、以下のように記述して再描画を行おうとしています。(以下、文字数制限を考慮して、必要な部分のみ取り出しています。)
java
1public class Designgame extends View { 2---省略 3public void setB(int Bnum){ 4 b = Bnum; 5} 6---省略 7protected void onDraw(Canvas c) { 8---省略 9paint.setColor(Color.argb(255, r, g, b)); 10Log.d("color", "");//ログで再描画されているか確認 11---省略 12} 13public void colchange() { 14 Log.d("colchange", "true");//ログでメソッドが呼び出されているか確認 15 setB(255); 16 invalidate(); 17} 18}
このようにカスタムビュー(と思われるもの)のファイルでは書いてあるのですが、コードに書いてあるようにログを入れて再描画されているか確認しました。すると、colchange()メソッドが呼び出されているのは確認できたのですが、onDraw()内のログが出力されていなかったためinvalidate();が効いていないことが分かりました。
説明が長くなりましたが対処方法を教えてください、お願いします。
###追記
調べた中で、階層を成しているViewは上層のものをinvalidate();しないという記事を発見しました。なので、以下の記述を操作するメインのjavaファイルに加えました。
※記述した場所は色々なメソッドを書いている部分の一部です
java
1@Override 2public boolean onTouchEvent(Motion event){ 3} 4---省略(様々なメソッド) 5public void activechange(){ 6PercentFrameLayout percentFrameLayout; 7 percentFrameLayout = (PercentFrameLayout)findViewById(R.id.pfl); 8 percentFrameLayout.invalidate(); 9}
しかし、以下のエラーが出ました。
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
なぜなのかわかりません。
・追記:抜けていたxmlファイルのidの記述を追加しました。
・追記:onCreateの部分を記述します。
java
1@Override 2 protected void onCreate(Bundle savedInstanceState) { 3 super.onCreate(savedInstanceState); 4 Intent i4 = getIntent(); 5 LR = i4.getStringExtra("LR"); 6 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 7 //setContentView(R.layout.activity_main); 8 //ステータスバー非表示 9 getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); 10 //タイトルバー非表示 11 requestWindowFeature(Window.FEATURE_NO_TITLE); 12 setNavigationbarHide(this, true); 13 14 LayoutInflater factory = LayoutInflater.from(this); 15 View view = factory.inflate(R.layout.activity_game, null); 16 setContentView(view); 17 18 Random rnd = new Random(); 19 WindowManager wm = (WindowManager)getSystemService(WINDOW_SERVICE); 20 Display dp = wm.getDefaultDisplay(); 21 Point po = new Point(); 22 dp.getSize(po); 23 winW = po.x; 24 winH = po.y; 25 perpower = 20; 26 spower = rnd.nextInt(perpower); 27 forspower = (int)(1000 * (1 / spower)); 28 rpower = winW - ((winW * 0.05) + (winW * spower));//右(利き)基準 29 lpower = (winW * 0.25) - (winW * spower);//左(利き)基準 30 stamina = 10 + rnd.nextInt(30);//時間耐久度 31 }
回答2件
あなたの回答
tips
プレビュー