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

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

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

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Q&A

解決済

1回答

1797閲覧

Androidでfinish()で、MainActivityに移動したところ、spinner()がなくなり、コメントの送信ボタンも押せなくなりました。

edoooooo

総合スコア476

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

0グッド

0クリップ

投稿2017/05/01 14:41

編集2017/05/01 18:51

Androidで、finish()でMainActivityに戻ったところ、spinnerが表示されなくなり、コメントの送信ボタンも押せなくなります。
また、Switchを押すと、Could not find a method onSwitchClicked(View)と表示されます。

どうすれば、本来の指定したlayoutに戻れるでしょうか?
finish()を
Intent intent = new Intent(SecondActivity, MainActivity.class);
startActivity(intent);
にかえると、layoutは、崩れたものとはならないのですが、
現在発生しているエラーのToastが永遠に繰り返して表示されます。

java

1 2 3public class LocationActivity extends FragmentActivity implements 4 GoogleApiClient.ConnectionCallbacks, 5 GoogleApiClient.OnConnectionFailedListener, 6 LocationListener,OnMapReadyCallback,GoogleMap.OnMapLongClickListener { 7 //onMapReadyをコールバックすれば自動でonMapLeadyが呼ばれる 8 9 10 @Override 11 protected void onCreate(Bundle savedInstanceState) { 12 super.onCreate(savedInstanceState); 13 setContentView(R.layout.activity_main); 14 } 15 16 17 @Override 18 protected void onStart() { 19 super.onStart(); 20 21 22 // LocationRequest を生成して精度、インターバルを設定 23 locationRequest = LocationRequest.create(); 24 locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 25 locationRequest.setInterval(1000); 26 locationRequest.setFastestInterval(16); 27 28 fusedLocationProviderApi = LocationServices.FusedLocationApi; 29 30 31 startFusedLocation(); 32 33 spinner = (Spinner) findViewById(R.id.spinner); 34 35 // ArrayAdapter 36 ArrayAdapter<String> adapter 37 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, spinnerItems); 38 39 adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 40 41 // spinner に adapter をセット 42 spinner.setAdapter(adapter); 43 // リスナーを登録 44 spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 45 // アイテムが選択された時 46 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 47 if (item.equals("GPSの現在地")) { 48 49 } 50 51 52 } 53 } 54 } 55 56 57 //送信ボタン 58 Button buttonTransmission = (Button) findViewById(R.id.buttonTransmission); 59 buttonTransmission.setOnClickListener(new View.OnClickListener() 60 61 { 62 @Override 63 public void onClick (View v){ 64 65 startFusedLocation(); 66 67 } 68 }); 69 70 71 //swtch 72 public void onSwitchClicked(View view) { 73 Switch swtOnOff = (Switch) view; 74 if (swtOnOff.isChecked()) { // ON状態になったとき 75 startFusedLocation(); 76 } else if (!(swtOnOff.isChecked())) { 77 stopFusedLocation(); 78 } 79 } 80 81 82 83 //startFusedLocation() 84 private void startFusedLocation() { 85 86 87 if (!mResolvingError) { 88 89 mGoogleApiClient = new GoogleApiClient.Builder(this) 90 .addApi(LocationServices.API) 91 .addConnectionCallbacks(this) 92 .addOnConnectionFailedListener(this) //failedListenerがある 93 .build(); 94 95 96 mGoogleApiClient.connect(); 97 98 } 99 100 } 101 102 private void stopFusedLocation() { 103 mGoogleApiClient.disconnect(); 104 } 105 106 @Override 107 protected void onStop() { 108 super.onStop(); 109 stopFusedLocation(); 110 } 111 112 113 @Override 114 public void onConnected(Bundle bundle) { 115 if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != 116 PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != 117 PackageManager.PERMISSION_GRANTED) { 118 return; 119 } 120 121 fusedLocationProviderApi = LocationServices.FusedLocationApi; 122 Location currentLocation = fusedLocationProviderApi.getLastLocation(mGoogleApiClient); 123 if (currentLocation != null && currentLocation.getTime() > 20000) { 124 125 location = currentLocation; 126 127 latitude = location.getLatitude(); 128 longitude = location.getLongitude(); 129 select(latitude, longitude); 130 131 } else { 132 try { 133 134 fusedLocationProviderApi.requestLocationUpdates(mGoogleApiClient, locationRequest, this); 135 Executors.newScheduledThreadPool(1).schedule(new Runnable() { 136 @Override 137 public void run() { 138 fusedLocationProviderApi.removeLocationUpdates(mGoogleApiClient, com.example.android.sample.near.LocationActivity.this); 139 } 140 }, 60000, TimeUnit.MILLISECONDS); 141 142 } catch (Exception e) { 143 System.out.println(e); 144 Toast toast = Toast.makeText(this, "例外が発生、位置情報のPermissionを許可していますか?", Toast.LENGTH_SHORT); 145 toast.show(); 146 147 //MainActivityに戻す 148 finish(); 149 } 150 } 151 } 152 153 154 155 @Override 156 public void onConnectionFailed(ConnectionResult connectionResult) { 157 if (mResolvingError) { 158 return; 159 } else if (connectionResult.hasResolution()) { 160 try { 161 mResolvingError = true; 162 connectionResult.startResolutionForResult(this, REQUEST_RESOLVE_ERROR); 163 } catch (IntentSender.SendIntentException e) { 164 mGoogleApiClient.connect(); 165 } 166 } else { 167 mResolvingError = true; 168 } 169 } 170} 171

xml

1<?xml version="1.0" encoding="utf-8"?> 2<LinearLayout 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 android:orientation="vertical" 8 android:background="#acf" 9 tools:context=".MainActivity"> 10 11 <LinearLayout 12 android:gravity="center" 13 android:background="#8af" 14 android:orientation="horizontal" 15 android:layout_margin="20dp" 16 android:layout_width="match_parent" 17 android:layout_height="wrap_content"> 18 19 --> 20 <Switch 21 android:id="@+id/swtOnOff" 22 android:text="GPS:" 23 android:textSize="18dp" 24 android:layout_margin="5dp" 25 android:layout_weight="1" 26 android:layout_width="0dp" 27 android:layout_height="wrap_content" 28 android:onClick="onSwitchClicked" /> 29 30 <Spinner 31 android:id="@+id/spinner" 32 android:layout_width="200dp" 33 android:layout_height="wrap_content" 34 android:layout_margin="20dp" 35 android:background="#fff" /> 36 37 38 39 <!--switchを一番ひだりにして、その右に地域選択するためのボタンや、お気に入りの星ボタンを作る 40 あとで 41 --> 42 43 44 </LinearLayout> 45 46 <ScrollView 47 android:id="@+id/scrollview" 48 android:layout_width="match_parent" 49 android:layout_height="0dp" 50 android:layout_weight="1" 51 android:layout_margin="20dp"> 52 53 54 <TextView 55 android:id="@+id/text_view" 56 android:layout_width="wrap_content" 57 android:layout_height="wrap_content" 58 android:textColor="#000" /> 59 </ScrollView> 60 61 <LinearLayout 62 android:gravity="center" 63 android:background="#8af" 64 android:orientation="horizontal" 65 android:layout_margin="20dp" 66 android:layout_width="match_parent" 67 android:layout_height="wrap_content" 68 android:layout_alignParentBottom="true"> 69 <EditText 70 android:id="@+id/Transmission" 71 android:layout_width="275dp" 72 android:layout_height="40dp" 73 android:hint="コメントを入力してください" 74 /> 75 76 <Button 77 android:id="@+id/buttonTransmission" 78 android:layout_width="60dp" 79 android:layout_height="40dp" 80 android:layout_toRightOf="@+id/Transmission" 81 android:text="送信" 82 /> 83 </LinearLayout> 84 85</LinearLayout>

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

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

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

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

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

yona

2017/05/01 16:30

自分の質問を読み返してください。なにも知らない回答者がわかると思いますか。
edoooooo

2017/05/01 16:49 編集

重要だと思われるコードを載せました。
guest

回答1

0

自己解決

Logで出力して気づいたのですが、
強制終了されてしまうため、finish()を呼ばないようにして、代わりにMainActivityへのStartActivityを呼びました。そうしたところ、エンドレスに何周も、エラーを繰り返しましたが、2回に1回は、正常に作動することがわかりました。

このような流れでした。
開始
connect 成功
select 成功
forが終わる。

またonCreate
/////////ここから
そして、fusedLocationが呼ばれ、
googleconnetを呼ぶ
mGoogleApiClient.conect()を呼び出される
そして、またfusedLocationが呼ばれる
そして、また、mGoogleApiClient.connectedが呼ばれる

そして、onConnectedがよばれる。
disconnectedでnot connectedとなり
そして、if(currentLocation!=null で、elseとなる。
catchでGoogleApiCliCient is not connected となる
finish()の代わりに、startActivityする

しかしまだ、2つ目のconnectedが残っており
unconnected nowとなり、
if(currentLocation!=null&& これで中に入れる
//////////ここの間に同じ動作が2回ずつ行われる

そして、selectして、それが終わると、

またonCreate()してfusedLocationが呼るという繰り返し、

この中で、///で囲んだところが2回同じ作業が繰り返されているため、
@onStart()のstartFusedLocation()を無くしました。そうしたところ、戻るボタンを押しても、layoutは、崩れず、xmlに書いたコード通りの画面を作ることができました。

投稿2017/05/01 21:01

edoooooo

総合スコア476

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問