我创建了一个包含两个活动的 Android 应用程序,但无法从第二个 activity 切换回第一个
I created an Android application with two activities, but cannot switch back from the second activity to the first
我创建了一个包含两个活动的 Android 应用程序,但是第二个 activity 上的按钮似乎无法正常工作。第一个 activity 上的按钮(一个地图 activity 和一个添加信息的按钮,它将用户带到第二个 activity 带有信息输入屏幕的按钮)工作正常,第二个 activity 上的按钮都没有=24=] 似乎有任何效果...我什至无法让 OnClick Toast 消息起作用。
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback , LocationListener {
public void AddMapInfo(View view) {
Intent intent = new Intent(this, InformationInputActivity.class);
LatLng providedLocation = current;
Bundle args = new Bundle();
args.putParcelable("provided_location", providedLocation);
intent.putExtra("bundle", args);
startActivityForResult(intent, 1);
}
public void onActivityResult (int requestCode, int resultCode, Intent data){
Toast.makeText(this, "back with cheese!", Toast.LENGTH_LONG).show();
if (requestCode != 1){
Toast.makeText(this, "Oops... This shouldn't happen", Toast.LENGTH_LONG).show();
}
else{
setContentView(R.layout.activity_maps);
InformationPoint informationPoint = (InformationPoint) data.getSerializableExtra("information_point");
informationPoint.display(mymap);
}
}
}
第二个 activity 带来了屏幕布局,它为 EditText 字段设置了正确的选项,所以我知道它获得了控制...但仅此而已。没有来自任何方法的 Toast 消息(当第一个 activity 收到位置更新信息时,我仍然得到 Toasts),而且单选按钮或提交按钮似乎都不起作用,我也是在第一个 activity 中没有得到结果和控制权。 (以前,我曾经能够在单选按钮上的选中选项上获得 Toast on ever,但现在这似乎也不起作用)。尝试发送 activity 结果会中止应用程序(因为我将 finish() 添加到第二个 activity)。
/* java */
package com.example.m.googlemapappfirst;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.maps.model.LatLng;
import org.w3c.dom.Text;
public class InformationInputActivity extends ActionBarActivity {
EditText editText;
String content = "";
LatLng providedLocation;
public InfoType infoType = InfoType.NO_INFO;
InformationPoint informationPoint;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_information_input);
Intent intent = getIntent();
Bundle bundle = getIntent().getParcelableExtra("bundle");
providedLocation = bundle.getParcelable("provided_location");
String message = String.valueOf(providedLocation);
editText = (EditText)findViewById(R.id.description);
editText.setHorizontallyScrolling(false);
editText.setMaxLines(5);
Toast.makeText(this, "enter cheese info", Toast.LENGTH_LONG);
}
public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
Toast.makeText(this,"you selected your cheese", Toast.LENGTH_LONG);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_information_input, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void AddPictureHandler(View view){
Toast.makeText(this, "i like to take pictures of cheese", Toast.LENGTH_LONG).show();
}
public void SubmitHandler(View view){
content = editText.getText().toString();
Toast.makeText(this, "add cheese info", Toast.LENGTH_LONG).show();
if(infoType == InfoType.NO_INFO){
Toast.makeText(this, "Please select an Information type", Toast.LENGTH_LONG).show();
}
else if(content.equals("")){
Toast.makeText(this, "Please write a description", Toast.LENGTH_LONG).show();
}
informationPoint = new InformationPoint(providedLocation, infoType, content);
Intent returnIntent = new Intent(this, MapsActivity.class);
returnIntent.putExtra("information_point", informationPoint);
this.setResult(1, returnIntent);
finish(); /* added after original post, this makes app crash consistently */
}
}
这是第二个 activity 的 xml 布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.example.m.googlemapappfirst.InformationInputActivity">
<TextView android:text="@string/instructions"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton android:id="@+id/radio_swiss"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/swiss"
android:onClick="onRadioButtonClicked"/>
<RadioButton android:id="@+id/radio_american"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/american"
android:onClick="onRadioButtonClicked"/>
<RadioButton android:id="@+id/radio_gruyere"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/gruyere"
android:onClick="onRadioButtonClicked"/>
<RadioButton android:id="@+id/radio_cheddar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/cheddar"
android:onClick="onRadioButtonClicked"/>
</RadioGroup>
<TextView android:text="@string/instructions2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/description"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:layout_weight="1"
android:hint="@string/type_here"
android:inputType="text"
android:imeOptions="actionDone"
android:maxLines ="4"
android:maxLength ="2000"
android:scrollHorizontally="false"
/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:orientation="horizontal">
<Button
android:id="@+id/take_picture_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Picture"
android:onClick="AddPictureHandler" />
<Button
android:id="@+id/submit_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Report Information"
android:onClick="SubmitHandler" />
</LinearLayout>
我没有看到任何问题,但在创建 toast 后缺少对 show() 的调用,就像这样:
Toast.makeText(this, "Oops... This shouldn't happen", Toast.LENGTH_LONG).show();
我创建了一个包含两个活动的 Android 应用程序,但是第二个 activity 上的按钮似乎无法正常工作。第一个 activity 上的按钮(一个地图 activity 和一个添加信息的按钮,它将用户带到第二个 activity 带有信息输入屏幕的按钮)工作正常,第二个 activity 上的按钮都没有=24=] 似乎有任何效果...我什至无法让 OnClick Toast 消息起作用。
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback , LocationListener {
public void AddMapInfo(View view) {
Intent intent = new Intent(this, InformationInputActivity.class);
LatLng providedLocation = current;
Bundle args = new Bundle();
args.putParcelable("provided_location", providedLocation);
intent.putExtra("bundle", args);
startActivityForResult(intent, 1);
}
public void onActivityResult (int requestCode, int resultCode, Intent data){
Toast.makeText(this, "back with cheese!", Toast.LENGTH_LONG).show();
if (requestCode != 1){
Toast.makeText(this, "Oops... This shouldn't happen", Toast.LENGTH_LONG).show();
}
else{
setContentView(R.layout.activity_maps);
InformationPoint informationPoint = (InformationPoint) data.getSerializableExtra("information_point");
informationPoint.display(mymap);
}
}
}
第二个 activity 带来了屏幕布局,它为 EditText 字段设置了正确的选项,所以我知道它获得了控制...但仅此而已。没有来自任何方法的 Toast 消息(当第一个 activity 收到位置更新信息时,我仍然得到 Toasts),而且单选按钮或提交按钮似乎都不起作用,我也是在第一个 activity 中没有得到结果和控制权。 (以前,我曾经能够在单选按钮上的选中选项上获得 Toast on ever,但现在这似乎也不起作用)。尝试发送 activity 结果会中止应用程序(因为我将 finish() 添加到第二个 activity)。
/* java */
package com.example.m.googlemapappfirst;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.maps.model.LatLng;
import org.w3c.dom.Text;
public class InformationInputActivity extends ActionBarActivity {
EditText editText;
String content = "";
LatLng providedLocation;
public InfoType infoType = InfoType.NO_INFO;
InformationPoint informationPoint;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_information_input);
Intent intent = getIntent();
Bundle bundle = getIntent().getParcelableExtra("bundle");
providedLocation = bundle.getParcelable("provided_location");
String message = String.valueOf(providedLocation);
editText = (EditText)findViewById(R.id.description);
editText.setHorizontallyScrolling(false);
editText.setMaxLines(5);
Toast.makeText(this, "enter cheese info", Toast.LENGTH_LONG);
}
public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
Toast.makeText(this,"you selected your cheese", Toast.LENGTH_LONG);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_information_input, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void AddPictureHandler(View view){
Toast.makeText(this, "i like to take pictures of cheese", Toast.LENGTH_LONG).show();
}
public void SubmitHandler(View view){
content = editText.getText().toString();
Toast.makeText(this, "add cheese info", Toast.LENGTH_LONG).show();
if(infoType == InfoType.NO_INFO){
Toast.makeText(this, "Please select an Information type", Toast.LENGTH_LONG).show();
}
else if(content.equals("")){
Toast.makeText(this, "Please write a description", Toast.LENGTH_LONG).show();
}
informationPoint = new InformationPoint(providedLocation, infoType, content);
Intent returnIntent = new Intent(this, MapsActivity.class);
returnIntent.putExtra("information_point", informationPoint);
this.setResult(1, returnIntent);
finish(); /* added after original post, this makes app crash consistently */
}
}
这是第二个 activity 的 xml 布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.example.m.googlemapappfirst.InformationInputActivity">
<TextView android:text="@string/instructions"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton android:id="@+id/radio_swiss"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/swiss"
android:onClick="onRadioButtonClicked"/>
<RadioButton android:id="@+id/radio_american"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/american"
android:onClick="onRadioButtonClicked"/>
<RadioButton android:id="@+id/radio_gruyere"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/gruyere"
android:onClick="onRadioButtonClicked"/>
<RadioButton android:id="@+id/radio_cheddar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/cheddar"
android:onClick="onRadioButtonClicked"/>
</RadioGroup>
<TextView android:text="@string/instructions2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/description"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:layout_weight="1"
android:hint="@string/type_here"
android:inputType="text"
android:imeOptions="actionDone"
android:maxLines ="4"
android:maxLength ="2000"
android:scrollHorizontally="false"
/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:orientation="horizontal">
<Button
android:id="@+id/take_picture_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Picture"
android:onClick="AddPictureHandler" />
<Button
android:id="@+id/submit_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Report Information"
android:onClick="SubmitHandler" />
</LinearLayout>
我没有看到任何问题,但在创建 toast 后缺少对 show() 的调用,就像这样:
Toast.makeText(this, "Oops... This shouldn't happen", Toast.LENGTH_LONG).show();