我創建了一個包含兩個活動的Android應用程序,但第二個活動的按鈕似乎無法正常工作。第一個活動的按鈕(一個地圖活動和一個添加信息的按鈕,用戶通過信息輸入屏幕接受第二個活動)工作正常,第二個活動的按鈕似乎都沒有任何影響......我不能甚至可以使OnClick Toast消息發揮作用。我創建了一個包含兩個活動的Android應用程序,但無法從第二個活動切換回第一個
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);
}
}
}
第二個活動帶來的佈局屏幕,並將其設置爲的EditText字段的正確選擇,所以我知道它得到控制......但僅此而已。沒有來自任何方法的Toast消息(當第一個活動接收到位置更新信息時,我仍然得到Toasts),單選按鈕或提交按鈕似乎都不起作用,我也沒有得到結果並控制第一次活動。 (之前,我曾經能夠在單選按鈕上的選項上得到Toast,但現在看起來也不起作用)。嘗試發送活動結果會中止應用程序(因爲我將finish()添加到第二個活動中)。
/* 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 */
}
}
這裏的第二個活動的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>
那麼在第二個活動中,您忘記將監聽器添加到按鈕中。你可以給我們從應用程序崩潰的堆棧跟蹤? – sonic
我是Android新手,如何獲取堆棧跟蹤?我所得到的只是「不幸的是,奶酪地圖應用已關閉,」只顯示的按鈕是「強制clsoe」。 (RUnning在設備上,模擬器無法在AMD筆記本電腦上工作。) – schwind
您可以在logcat中看到您的應用程序的日誌。您可以通過不同的方式訪問它,具體取決於IDE(甚至命令行)。在這個視圖中,你會看到很多消息和任何未捕獲的異常(如導致你的應用程序崩潰的異常)堆棧跟蹤 – sonic