###前提・実現したいこと
グローバル変数を使ってデータのやり取りをしたい。
###発生している問題・エラーメッセージ
エラーはないですが、エミュレータを使って動かすと強制終了する。
###該当のソースコード
manifests.xml
XMl
1<?xml version="1.0" encoding="utf-8"?> 2<manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="numa.jyanken"> 4 5 <application 6 android:allowBackup="true" 7 android:icon="@drawable/icon" 8 android:label="@string/app_name" 9 android:roundIcon="@drawable/icon" 10 android:supportsRtl="true" 11 android:theme="@style/AppTheme" 12 android:name=".Globals"> 13 <activity android:name=".JyankenActivity" 14 android:label="@string/app_name"> 15 <intent-filter> 16 <action android:name="android.intent.action.MAIN" /> 17 18 <category android:name="android.intent.category.LAUNCHER" /> 19 </intent-filter> 20 </activity> 21 <activity android:name=".KekkaActivity" 22 android:label="@string/app_name"> 23 </activity> 24 25 <activity android:name=".KekkaActivity2" 26 android:label="@string/app_name"> 27 </activity> 28 29 <activity android:name=".KekkaActivity3" 30 android:label="@string/app_name"> 31 </activity> 32 33 34 </application> 35 36</manifest>
Globals.Java
Java
1package numa.jyanken; 2 3import android.app.Application; 4 5/** 6 * Created by user on 2017/04/30. 7 */ 8 9public class Globals extends Application { 10 11 int i=0; //使う 12 int p; 13 int syouritu; 14 15}
KekkaActivity.Java
Java
1package numa.jyanken; 2 3import android.app.Activity; 4import android.content.Intent; 5import android.os.Bundle; 6import android.util.Log; 7import android.view.View; 8import android.widget.Button; 9import android.widget.ImageView; 10import android.widget.TextView; 11 12/** 13 * Created by user on 2017/04/26. 14 */ 15 16public class KekkaActivity extends Activity { 17 18 @Override 19 protected void onCreate(Bundle savedInstanceState) { 20 super.onCreate(savedInstanceState); 21 setContentView(R.layout.kekka); 22 final ImageView imageView1 = (ImageView) findViewById(R.id.imageView); 23 final ImageView imageView2 = (ImageView) findViewById(R.id.imageView2); 24 Dousa dousa = new Dousa(); 25 final TextView textview = (TextView) findViewById(R.id.textView1); 26 final TextView textview2 = (TextView) findViewById(R.id.textView2); 27 final TextView textview3 = (TextView) findViewById(R.id.textView3); 28 29 Globals globals=(Globals)this.getApplication(); //インスタンス化 30 31 dousa.judge(); 32 33 int Myplayer=0; 34 int Youplayer = dousa.player[1]; 35 int Myhand = R.string.gu; 36 int Hikiwake = R.string.hikiwake; 37 int Lose = R.string.lose; 38 int Win = R.string.win; 39 int Youhand = dousa.hand; 40 41 if (Myplayer - Youplayer == 0) { 42 imageView1.setImageResource(R.drawable.gu); 43 imageView2.setImageResource(R.drawable.gu); 44 textview2.setText(Myhand); 45 textview3.setText(Youhand); 46 textview.setText(Hikiwake); 47 TextView textView4 = (TextView)findViewById(R.id.text); 48 textView4.setText(globals.i); //表示 49 } else if (Myplayer - Youplayer == -2) { 50 imageView1.setImageResource(R.drawable.gu); 51 imageView2.setImageResource(R.drawable.pa); 52 textview2.setText(Myhand); 53 textview3.setText(Youhand); 54 textview.setText(Lose); 55 TextView textView4 = (TextView)findViewById(R.id.text); 56 textView4.setText(globals.i); //表示 57 } else if (Myplayer - Youplayer == -1) { 58 globals.i=globals.i+1; //値を変える 59 imageView1.setImageResource(R.drawable.gu); 60 imageView2.setImageResource(R.drawable.cyoki); 61 textview2.setText(Myhand); 62 textview3.setText(Youhand); 63 textview.setText(Win); 64 TextView textView4 = (TextView)findViewById(R.id.text); 65 textView4.setText(globals.i); //表示 66 } 67 68 69 Button btn4 = (Button) findViewById(R.id.button); 70 btn4.setOnClickListener(new View.OnClickListener() { 71 @Override 72 public void onClick(View v) { 73 Intent intent = new Intent(KekkaActivity.this,JyankenActivity.class); 74 startActivity(intent); 75 finish(); 76 } 77 78 }); 79 80 81 } 82} 83
グローバル変数を使うところにはコメントが書いてあります。
お力をお貸しください。
###補足情報(言語/FW/ツール等のバージョンなど)
androidstudio2.3
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。