gpt4 book ai didi

java - 基本 Intent 问题?

转载 作者:行者123 更新时间:2023-12-01 11:01:32 26 4
gpt4 key购买 nike

我正在尝试构建一个应用程序,在按下按钮时显示第二个 Activity 。它还根据第一个 Activity 的 EditText (id:ApplesInput) 中输入的文本更改第二个 Activity (id:baconTextId) 中的 TextView 文本。

Activity 1:Apples.java Activity 2:Bacon.java

在我放置的 xml 中的两个按钮上

android:onClick = "Thismethodiscalledonclick"

这样我就不需要添加监听器了。

我正在关注这个tutorial Intent ,但出现错误。 Android Studio 显示没有错误,但是当我按下带有“ApplesInput”id 的“ApplesInput”按钮时,我的手机显示“不幸的是 Intent 示例已停止。”

enter image description here

按下此按钮后,我应该看到如下所示的屏幕:

enter image description here

第一个 Activity :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);
}
}

第二个 Activity :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)

如果您认为解决问题的过程中需要更多代码或资源,请随时向我询问。

最佳答案

试试这个:

    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

关于java - 基本 Intent 问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33294114/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com