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>
回答1件
あなたの回答
tips
プレビュー