android studio试图在我的代码中返回主活动错误

时间:2016-03-26 04:31:07

标签: java android-studio

从MainActivity类到我的TimeIn类没有任何问题,但是我在回到主要活动时遇到了问题,这是我的Java类文件TimeIn:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;

public class TimeIn extends Activity {

@Override
protected void onCreate (Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.timeinlayout);
    final Globals g = (Globals)getApplication();
    final int timestart=g.getData1();
    final ImageButton btn1 = (ImageButton) findViewById(R.id.button1);
    btn1.setOnClickListener(new OnClickListener() {

        public int i = 0;

        public void onClick(View v) {

            if (i == 0 && timestart == 0) {
                btn1.setImageResource(R.drawable.lgbtn1);
                g.setData1(1);
                i = 1;
            }
            if (i == 0 && timestart == 1) {
                btn1.setImageResource(R.drawable.lgbtn);
                g.setData1(0);
                i = 1;
            }
            Intent punch2 = new Intent(this, MainActivity.class);
            startActivity(punch2);
        }
    });
    final ImageButton btn2 = (ImageButton) findViewById(R.id.button2);
    btn2.setOnClickListener(new OnClickListener() {
        public int j = 0;
        Globals g = (Globals) getApplication();
        final int breakstart = g.getData2();

        public void onClick(View v) {
            if (j == 0 && breakstart == 0) {
                btn2.setImageResource(R.drawable.lgbtn3);
                g.setData2(1);
                j = 1;
            }
            if (j == 0 && breakstart == 1) {
                btn2.setImageResource(R.drawable.lgbtn2);
                g.setData2(0);
                j = 1;
            }
            Intent punch3 = new Intent(this, MainActivity.class);
            startActivity(punch3);
        }
    });
}
}

它不会以这种方式编译,代码错误说: 无法解析构造函数'intent(Java .lang.Class< com.package.appname>)'

但如果我按照各自的目的移动两个意图:

//previous onClick 1st codeblock
});
Intent punch2 = new Intent(this, MainActivity.class);
startActivity(punch2);
//previous onClick 2nd codeblock
});
Intent punch3 = new Intent(this, MainActivity.class);
startActivity(punch3);
}
}

它有效,但是立即回到主要活动,我希望能够先点击btn1或btn2,这应该从我认为的点击方法中触发,然后返回到MainActivity。我是Java的新手,任何帮助都会受到赞赏,这可能是一件简单的事情,我做错了。

1 个答案:

答案 0 :(得分:0)

我弄清楚我做错了什么,我不得不拆分代码,所以我在onCreate方法中定义了意图,如下所示:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;

public class TimeIn extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.timeinlayout);
        final Globals g = (Globals) getApplication();
        final int timestart = g.getData1();
        final int breakstart = g.getData2();

        final Intent punch2 = new Intent(this, MainActivity.class);/* this is where i defined the intent, and you have to declair it as final*/

        final ImageButton btn1 = (ImageButton) findViewById(R.id.button1);
        final ImageButton btn2 = (ImageButton) findViewById(R.id.button2);
        if (timestart == 0){btn1.setImageResource(R.drawable.lgbtn);}
        if (timestart == 1){btn1.setImageResource(R.drawable.lgbtn1);}
        if (breakstart == 0){btn2.setImageResource(R.drawable.lgbtn2);}
        if (breakstart == 1){btn2.setImageResource(R.drawable.lgbtn3);}

然后在每个onClick方法中使用下半部分,如下所示:

            btn1.setOnClickListener(new OnClickListener() {

            public int i = 0;

            public void onClick(View v) {

                if (i == 0 && timestart == 0) {
                    btn1.setImageResource(R.drawable.lgbtn1);
                    g.setData1(1);
                    i = 1;
                }
                if (i == 0 && timestart == 1) {
                    btn1.setImageResource(R.drawable.lgbtn);
                    g.setData1(0);
                    i = 1;
                }
                startActivity(punch2);//starts the new activity after button click tasks
                return;
            }
        });
        btn2.setOnClickListener(new OnClickListener() {
            public int j = 0;
            Globals g = (Globals) getApplication();


            public void onClick(View v) {
                final ImageButton btn2 = (ImageButton) findViewById(R.id.button2);
                if (j == 0 && breakstart == 0) {
                    btn2.setImageResource(R.drawable.lgbtn3);
                    g.setData2(1);
                    j = 1;
                }
                if (j == 0 && breakstart == 1) {
                    btn2.setImageResource(R.drawable.lgbtn2);
                    g.setData2(0);
                    j = 1;
                }
                startActivity(punch2);
                return;
            }
        });


    }

}

所以事实证明这是我的错,所以我希望这有助于其他人开始学习Java