Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/218.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 安卓-清除意图_Java_Android_Android Intent - Fatal编程技术网

Java 安卓-清除意图

Java 安卓-清除意图,java,android,android-intent,Java,Android,Android Intent,我的问题与这个问题有关 但我在实施上遇到了问题 我今天的第一堂课是java,它有一个按钮,当我点击按钮时,我会输入额外的intent.putExtra(“highlegs”,“highlegs”)和起始触觉(意图) TodaySercise.java final Intent intent = getIntent(); if (intent != null) { String clicked = intent.getStringExtra("button");

我的问题与这个问题有关 但我在实施上遇到了问题

我今天的第一堂课是java,它有一个按钮,当我点击按钮时,我会输入额外的
intent.putExtra(“highlegs”,“highlegs”)
起始触觉(意图)

TodaySercise.java

final Intent intent = getIntent();
    if (intent != null) {
        String clicked = intent.getStringExtra("button");

        if (clicked.equals("btn1")) {

            intent.setClass(TodaysExercise.this, DoExercise.class);

            button.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    //When btn1 is clicked I putExtra
                    intent.putExtra("highlegs", "High Legs");
                    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);

                }
            });


        }
    }
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_do_the_exercise);
    textView = (TextView) findViewById(R.id.replace_this);
    descheader = (TextView) findViewById(R.id.desc_header_id);
    fab = (FloatingActionButton) findViewById(R.id.fab);

    Typeface custom_font = Typeface.createFromAsset(getAssets(), "fonts/countdown.ttf");
    descheader.setTypeface(custom_font);


    toolbar = (Toolbar) findViewById(R.id.app_bar);
    setSupportActionBar(toolbar);

    Bundle bundle = getIntent().getExtras();
    if (bundle != null) {
        String text = bundle.getString("highlegs");


        if (text != null)
            textView.setText(text);

    }

    if (textView.getText().equals("High Legs")) 
    {

        ImageView imLoading = (ImageView) findViewById(R.id.loadingView);
        imLoading.setBackgroundResource(R.drawable.workout);
        AnimationDrawable frameAnimation = (AnimationDrawable) imLoading
                .getBackground();
        frameAnimation.start();


        final Intent i = new Intent();
        i.setClass(DoExercise.this, ReadyForNext.class);
        fab.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                i.putExtra("next", "Leg Curls X 20");
                startActivity(i);
                i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);

            }
        });


    }
下一个类
DoExercise.java
然后获取字符串
string text=bundle.getString(“highlegs”)
并将其设置为textView
textView.setText(text)。然后我检查TextView是否等于
if(TextView.getText().equals(“High Legs”)
,如果是,我再次输入extra
I.putExtra(“next”,“Leg Curls X 20”)并开始下一节课
开始活动(意图)

DoExercise.java

final Intent intent = getIntent();
    if (intent != null) {
        String clicked = intent.getStringExtra("button");

        if (clicked.equals("btn1")) {

            intent.setClass(TodaysExercise.this, DoExercise.class);

            button.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    //When btn1 is clicked I putExtra
                    intent.putExtra("highlegs", "High Legs");
                    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);

                }
            });


        }
    }
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_do_the_exercise);
    textView = (TextView) findViewById(R.id.replace_this);
    descheader = (TextView) findViewById(R.id.desc_header_id);
    fab = (FloatingActionButton) findViewById(R.id.fab);

    Typeface custom_font = Typeface.createFromAsset(getAssets(), "fonts/countdown.ttf");
    descheader.setTypeface(custom_font);


    toolbar = (Toolbar) findViewById(R.id.app_bar);
    setSupportActionBar(toolbar);

    Bundle bundle = getIntent().getExtras();
    if (bundle != null) {
        String text = bundle.getString("highlegs");


        if (text != null)
            textView.setText(text);

    }

    if (textView.getText().equals("High Legs")) 
    {

        ImageView imLoading = (ImageView) findViewById(R.id.loadingView);
        imLoading.setBackgroundResource(R.drawable.workout);
        AnimationDrawable frameAnimation = (AnimationDrawable) imLoading
                .getBackground();
        frameAnimation.start();


        final Intent i = new Intent();
        i.setClass(DoExercise.this, ReadyForNext.class);
        fab.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                i.putExtra("next", "Leg Curls X 20");
                startActivity(i);
                i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);

            }
        });


    }
我得到
stringtext=bundle.getString(“下一步”)
和set
tv_nextdesc.setText(text)字符串附加到文本视图。我在这个类中还有一个按钮,应该返回到
DoExercise.java
,这就是我的问题所在

ReadyForNext.java

public class ReadyForNext extends AppCompatActivity {
Button btn_next_exercise;
TextView tv_nextdesc;
TextView tv_nexttxt;
Context f;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_ready_for_next);
    btn_next_exercise = (Button) findViewById(R.id.btn_next_exercise);
    tv_nextdesc = (TextView) findViewById(R.id.tv_nextdesc);
    tv_nexttxt = (TextView) findViewById(R.id.nxtTxt);

    Typeface custom_font = Typeface.createFromAsset(getAssets(), "fonts/countdown.ttf");
    tv_nextdesc.setTypeface(custom_font);
    tv_nexttxt.setTypeface(custom_font);


    Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            String text = bundle.getString("next");
            if (text != null)
                tv_nextdesc.setText(text);

        }


    final Intent i = new Intent();
    i.setClass(ReadyForNext.this, DoExercise.class);
    btn_next_exercise.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            //I first check what the title is to determine what to put extra
            if (tv_nextdesc.getText().equals("Leg Curls X 20")) {
                i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
                //then here I putExtra
                i.putExtra("legcurls", "Leg Curls");
                startActivity(i);
            }
        }
    });

}
正如你所见,我有三个类基本上是循环运行的,
todaysercise.java
打开
DoExercise.java
,它打开
ReadyForNext.java
,从这里我想再次打开
DoExercise.java
(重用DoExercise.java),但那时候我想清除添加新意图的意图


关于如何实现这一点有什么帮助吗?

请在此处完成您的DoExercise活动

fab.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            i.putExtra("next", "Leg Curls X 20");
            startActivity(i);
            i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
            finish();

        }
    });
像你现在做的那样,从ReadyForNext活动开始

现在您必须在DoExercise类中进行一些更改,因为您的意图是在不同的类中更改额外的键,否则您将得到null pointerException

Bundle bundle = getIntent().getExtras();
if (bundle != null) {
    if(bundle.containsKey("highlegs")){
    String text = bundle.getString("highlegs");
    }
    if(bundle.containsKey("legcurls")){
    String text = bundle.getString("legcurls");
    }


    if (text != null)
        textView.setText(text);

}

希望这能对你有所帮助。

在这里完成你的DoExercise活动

fab.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            i.putExtra("next", "Leg Curls X 20");
            startActivity(i);
            i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
            finish();

        }
    });
像你现在做的那样,从ReadyForNext活动开始

现在您必须在DoExercise类中进行一些更改,因为您的意图是在不同的类中更改额外的键,否则您将得到null pointerException

Bundle bundle = getIntent().getExtras();
if (bundle != null) {
    if(bundle.containsKey("highlegs")){
    String text = bundle.getString("highlegs");
    }
    if(bundle.containsKey("legcurls")){
    String text = bundle.getString("legcurls");
    }


    if (text != null)
        textView.setText(text);

}

希望这能对您有所帮助。

您的活动被调整为:A->B->C,然后您希望删除C并更新活动B。建议1:如果这三个屏幕共同负责单个任务,最好使用片段而不是活动。使用片段处理这种情况会更容易。建议2:从B->C移动时,调用this.finish();在活动B和活动C中,使用一组新的bundle向C激发一个新的意图
textView.setText(text)B->C,然后您希望删除C并更新活动B。建议1:如果这三个屏幕共同负责单个任务,最好使用片段而不是活动。使用片段处理这种情况会更容易。建议2:从B->C移动时,调用this.finish();在活动B和活动C中,使用一组新的bundle向C激发一个新的意图
textView.setText(text)legcolls
?在您的代码中,您只声明它是否是,这意味着它将始终是
highlegs
,并且
Legcolls
将永远不会被调用?我已经让它工作了,谢谢。我使用了您的代码并放置了
stringtext2=bundle.getString(“legcolls”)如果(bundle!=null){
但是我在哪里获取字符串和settext到
legcolls
?在你的代码中,你只声明如果它是,这意味着它将永远是
highlegs
并且
legcolls
将永远不会被调用?我让它工作了,谢谢你。我使用了你的代码并放置了
String text2=bundle.getstring(如果(bundle!=null){