我正在尝试构建一个应用程序,在按下按钮时显示第二个活动。它还根据在第一个活动的EditText(id:ApplesInput)中输入的文本更改第二个活动(id:baconTextId)中TextView的文本。
活动1:Apples.java 活动2:Bacon.java
在xml的两个按钮上我放
android:onClick = "Thismethodiscalledonclick"
这样我就不需要添加监听器了。
我在意图上遵循此tutorial,但收到了错误。 Android Studio没有显示任何错误,但是当我按下“ApplesInput”按钮时,我的手机上写着“ApplesInput”,“不幸的是,Intent示例已停止。”
按下此按钮后,我应该得到如下屏幕:
第一项活动:Apples.java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.content.Intent;
import android.view.*;
import android.widget.*;
public class Apples extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_apples);
}
public void Thismethodiscalledonclick(View v){
Intent i = new Intent(this, Bacon.class);
final EditText ApplesInput = (EditText) findViewById(R.id.ApplesInput); //Named var and id same for simplicity
String usersmessage = ApplesInput.getText().toString(); //Whatever user types
i.putExtra("applesMessageKey", usersmessage); //Parameters == ("What do you want to call this", What piece of information?)
startActivity(i); //Starts intent stuff
}
@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_apples, 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);
}
}
第二项活动:Bacon.java
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.*;
public class Bacon extends AppCompatActivity {
final TextView BaconText = (TextView) findViewById(R.id.BaconTextid);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bacon);
Bundle applesData = getIntent().getExtras(); //Call intent and store the extras in applesdata
if(applesData == null){
BaconText.setText("Must type something, sending you back...");
Thismethodiscalledonclick(null); //Take them back to first screen
return;
}
String applesDataReceived = applesData.getString("appleMessageKey");
BaconText.setText(applesDataReceived);
}
public void Thismethodiscalledonclick(View v){
Intent i = new Intent(this, Apples.class);
startActivity(i);
}
@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_bacon, 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);
}
}
过滤了一些logcat错误:
android.provider.Settings$SettingNotFoundException: accessibility_enabled
at android.provider.Settings$Secure.getIntForUser(Settings.java:3163)
at android.provider.Settings$Secure.getInt(Settings.java:3148)
at com.android.systemui.power.PowerUI$1.onReceive(PowerUI.java:386)
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:774)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5272)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:883)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
at dalvik.system.NativeStart.main(Native Method)
如果您认为在解决问题的过程中需要代码或资源,请随时向我询问。
答案 0 :(得分:1)
试试这个:
private TextView BaconText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bacon);
BaconText = (TextView) findViewById(R.id.baconText);
Bundle applesData = getIntent().getExtras(); //Call intent and store the extras in applesdata
if(applesData == null){
BaconText.setText("Must type something, sending you back...");
Thismethodiscalledonclick(null); //Take them back to first screen
return;
}
String applesDataReceived = applesData.getString("applesMessageKey");
BaconText.setText(applesDataReceived);
}
public void Thismethodiscalledonclick(View v){
Intent i = new Intent(this, MainActivity.class);
startActivity(i);
}
在onCreate()中调用findViewById()。 ApplesActivity中的putExtra名称为“applesMessageKey”,BaconActivity中的名称getString为“appleMessageKey”。他们不一样:D