奇怪的两个活动开始

时间:2011-11-03 15:41:58

标签: android android-intent

我的活动意图启动器出现问题。看来,当我运行这段代码时

Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setClass(this, contentScreen.class);
                startActivity(intent);
                finish();

在我的autoGO方法中,它运行两个活动,我知道这是因为我的logcat告诉我这个:

11-03 11:16:16.542: INFO/ActivityManager(58): Displayed activity three.three/.contentScreen: 2030 ms (total 3391 ms)
11-03 11:16:16.713: INFO/ActivityManager(58): Displayed activity three.three/.contentScreen: 1596 ms (total 1596 ms)

如果数据库中的特定值为true,我的流程基本上就是这个活动,它会自动运行autoGO,autoGO会在autoGO中启动一个新意图并带我到那个类。然而,我似乎在某处开始两项活动。结果我打开了两个相同类型的活动,但这是不需要的。这可能是什么原因?

这是autoGO方法:

public void autoGO(View arg1) throws IOException,
XmlPullParserException {

    EditText username = (EditText) findViewById(R.id.widget40);
    EditText password = (EditText) findViewById(R.id.widget37);

    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
    //gainString(remembered);
    setUserPass();
    System.out.println(Compare[1]);
    System.out.println(Compare[2]);
    request.addProperty("User", Compare[1]);
    request.addProperty("Password", Compare[2]);
    autoSign = false;
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
            SoapEnvelope.VER11);
    envelope.dotNet = true;
    envelope.setOutputSoapObject(request);
    AndroidHttpTransport httpTransport = new AndroidHttpTransport(URL);
    httpTransport.debug = true;
    httpTransport.call(SOAP_ACTION, envelope); // send request
    SoapObject result = (SoapObject) envelope.getResponse(); // get
    System.out.println(result);

    int Conversion = Integer.parseInt(result.getProperty(0).toString());
    System.out.println(Conversion);
    if (Conversion > 0) {

        try{

            sqlDB=openOrCreateDatabase(DBNAME1,Context.MODE_PRIVATE,null);
            System.out.println("open or create database");
            //sqlDB.execSQL("UPDATE " + Table1 + " SET NAME = '" + sUserName + "' WHERE NAME = '" + remembered[1] + "'");
            System.out.println("URL updated");
            //sqlDB.execSQL("UPDATE " + Table1 + " SET PASSWORD = '" + sPassword + "' WHERE PASSWORD = '" + remembered[2]+ "'");
            sqlDB.execSQL("UPDATE " + Table1 + " SET URL = '" + "http://" + result.getProperty(2).toString()+ "'");
            System.out.println("HERE IS THE NEW URL " + "http://" + result.getProperty(2).toString());

            contentScreen co = new contentScreen();
            co.signAuto(true);

            Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setClass(this, contentScreen.class);
                startActivity(intent);
                finish();
        }


             catch (Exception e1) {
                // TODO Auto-generated catch block
                 System.out.println("Problem "+ e1.getMessage());
                e1.printStackTrace();
            }
            finally
            {
                sqlDB.close();

            }

    } else {
        username.setTextSize(10);
        username.setText("Invalid username or passcode!");
    }
}

以下是完整代码:

    public class Activity1 extends Activity {
/** Called when the activity is first created. */
JoshTwoActivity main;
Activity1 Activity1;
Activity2 two;

boolean checkTick = false;
private static final String NAMESPACE = "http://tempuri.org/";
private static final String URL = "http://xxxxxxxxxxxxxxxxxxxxxx/Webservice/xxxxxxxxxxxe.asmx?WSDL";
private static final String SOAP_ACTION = "http://tempuri.org/ValidateUser";
private static final String METHOD_NAME = "ValidateUser";
static String[] Compare = {"First time login, please enter name","pass","http://default.com"};

boolean tickBoxes [] = {false, false};
private String[] remembered = { "", "", "", "" };
View arg3;
Boolean autoSign = false;
SQLiteDatabase sqlDB;

private static String DBNAME1= "database1.db";
private static String DBNAME2= "database2.db";
private static String Table1= "People";
private static String Table2= "options";


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    setRequestedOrientation(1);

    final EditText username = (EditText) findViewById(R.id.widget40);
    final EditText password = (EditText) findViewById(R.id.widget37);

    obtainDetails();
    getValues();


    final CheckBox checkBox = (CheckBox) findViewById(R.id.widget35);
    checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
            if (checkBox.isChecked()) {
                tickBoxes[0]=true;
                updateFields();

            }
            if (!checkBox.isChecked()) {

                tickBoxes[0]=false;
                updateFields();
            }
        }
    });
    // NEW SAVE CHECKBOX IS HERE
    final CheckBox saveBox = (CheckBox) findViewById(R.id.widget36);
    saveBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
            if (saveBox.isChecked()) {
                tickBoxes[1]=true;
                updateFields();

            }
            if (!saveBox.isChecked()) {
                tickBoxes[1]=false;
                System.out.println(tickBoxes[1]);
                updateFields();
            }
        }
    });
    // FOR LOOP TO GO HERE, AND ALSO REMOVAL OF CHECKTICK
    for (int i = 0; i < tickBoxes.length; i++) {
        if (tickBoxes[1]) {
            saveBox.setChecked(true);

            if(!tickBoxes[0]){

            try {
                //gainString(remembered);

                setUserPass();

                username.setText(remembered[1]);
                System.out.println(remembered[1]);
                password.setText(remembered[2]);

            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            }

            // SET THE TEXT HERE IF NEEDED
        }
        if (tickBoxes[0]) {
            checkBox.setChecked(true);
            try {
                autoSign = true;
                System.out.println(autoSign);
                autoGO(arg3);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (XmlPullParserException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }

    final Button buttonEXIT = (Button) findViewById(R.id.widget41);
    buttonEXIT.setOnClickListener(new ViewStub.OnClickListener() {
        @Override
        public void onClick(View arg3) {
            username.setText("");
            password.setText("");
            checkBox.setChecked(false);
            saveBox.setChecked(false);
            updateFields();

        }
    });

    layoutControl();

}

public void destory(boolean newterminate) {
    main.sessionDestroy(newterminate);
}

public void layoutControl() {


    final Button buttonLOGIN = (Button) findViewById(R.id.widget34);
    buttonLOGIN.setOnClickListener(new ViewStub.OnClickListener() {
        @Override
        public void onClick(View arg1) {
            // TODO Auto-generated method stub
            try {
                validateUser(arg1);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (XmlPullParserException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    });

    final Button signUP = (Button) findViewById(R.id.widget32);
    signUP.setOnClickListener(new ViewStub.OnClickListener() {
        @Override
        public void onClick(View arg4) {
            // TODO Auto-generated method stub
            // startActivity(launchBrowser);
            Intent myIntent = new Intent(arg4.getContext(), login.class);
            startActivityForResult(myIntent, 0);
            finish();
        }
    });

}
public void updateFields()
{
    String one;
    String two;


    if(tickBoxes[0]){
        one = "true";
        two = "false";
    }
    else{
        one = "false";
        two = "true";
    }

    String three;
    String four;
    if(tickBoxes[1])
    {
        three = "true";
        four = "false";
    }
    else
    {
        three = "false";
        four = "true";
    }
    System.out.println("badadfasdfa" +one);
    System.out.println("badadfasdfa" +two);

    try{
        sqlDB= openOrCreateDatabase(DBNAME2,Context.MODE_PRIVATE,null);



        System.out.println("values updated");
        sqlDB.execSQL("UPDATE " + Table2 + " SET SaveP= '" + three + "' WHERE SaveP = '" + four + "'");
        sqlDB.execSQL("UPDATE " + Table2 + " SET Signauto= '" + one+ "' WHERE Signauto = '" + two+ "'");
        sqlDB.close();
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
    finally
    {
        sqlDB.close();
    }

}

public void validateUser(View arg1) throws IOException,
        XmlPullParserException {
    EditText username = (EditText) findViewById(R.id.widget40);
    EditText password = (EditText) findViewById(R.id.widget37);

    String sUserName, sPassword;
    sUserName = username.getText().toString();
    sPassword = password.getText().toString();
    System.out.println(sPassword + " USERJHJERJ");
    if(!autoSign){
    if (sUserName.length() <= 2 || sPassword.length() <= 2) {
        username.setTextSize(10);
        username.setText("enter both fields!");

    } else {
        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); // set
        request.addProperty("User", sUserName); // variable name, value. I
                                                // got
        request.addProperty("Password", sPassword);

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                SoapEnvelope.VER11); // put all required data into a soap
        // envelope
        envelope.dotNet = true;
        envelope.setOutputSoapObject(request); // prepare request
        AndroidHttpTransport httpTransport = new AndroidHttpTransport(URL);
        httpTransport.debug = true;
        httpTransport.call(SOAP_ACTION, envelope); // send request

        System.out.println("HERE IS THE ENVELOPE "
                + envelope.getInfo("User", "Password"));
        SoapObject result = (SoapObject) envelope.getResponse(); // get
        System.out.println(result);

        // String checkPass =
        int Conversion = Integer.parseInt(result.getProperty(0).toString());
        System.out.println(Conversion);
        if (Conversion > 0) {
            try{

            sqlDB=openOrCreateDatabase(DBNAME1,Context.MODE_PRIVATE,null);
            System.out.println("open or create database");
            sqlDB.execSQL("UPDATE " + Table1 + " SET NAME = '" + sUserName + "' WHERE NAME = '" + Compare[1] + "'");
            System.out.println("username updated");
            sqlDB.execSQL("UPDATE " + Table1 + " SET PASSWORD = '" + sPassword + "' WHERE PASSWORD = '" + Compare[2]+ "'");
            sqlDB.execSQL("UPDATE " + Table1 + " SET URL = '" + "http://" + result.getProperty(2).toString() + "' WHERE URL = '" + Compare[0] + "'");
            Intent myIntent = new Intent(arg1.getContext(),
                    contentScreen.class);

            startActivityForResult(myIntent, 0);
            finish();
            }
            /*eStrings[0] = result.getProperty(0).toString();
            eStrings[1] = sUserName;
            eStrings[2] = sPassword;
            eStrings[3] = "http://" + result.getProperty(2).toString();*/

             catch (Exception e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
                System.out.println(e1.toString() + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
            }
            finally
            {
                sqlDB.close();
            }
        } else {
            username.setTextSize(10);
            username.setText("Invalid username or passcode!");
        }

    }}
    //}else{

    //}

}

public void autoGO(View arg1) throws IOException,
XmlPullParserException {

    EditText username = (EditText) findViewById(R.id.widget40);
    EditText password = (EditText) findViewById(R.id.widget37);

    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
    //gainString(remembered);
    setUserPass();
    System.out.println(Compare[1]);
    System.out.println(Compare[2]);
    request.addProperty("User", Compare[1]);
    request.addProperty("Password", Compare[2]);
    autoSign = false;
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
            SoapEnvelope.VER11);
    envelope.dotNet = true;
    envelope.setOutputSoapObject(request);
    AndroidHttpTransport httpTransport = new AndroidHttpTransport(URL);
    httpTransport.debug = true;
    httpTransport.call(SOAP_ACTION, envelope); // send request
    SoapObject result = (SoapObject) envelope.getResponse(); // get
    System.out.println(result);

    int Conversion = Integer.parseInt(result.getProperty(0).toString());
    System.out.println(Conversion);
    if (Conversion > 0) {

        try{

            sqlDB=openOrCreateDatabase(DBNAME1,Context.MODE_PRIVATE,null);
            System.out.println("open or create database");
            //sqlDB.execSQL("UPDATE " + Table1 + " SET NAME = '" + sUserName + "' WHERE NAME = '" + remembered[1] + "'");
            System.out.println("URL updated");
            //sqlDB.execSQL("UPDATE " + Table1 + " SET PASSWORD = '" + sPassword + "' WHERE PASSWORD = '" + remembered[2]+ "'");
            sqlDB.execSQL("UPDATE " + Table1 + " SET URL = '" + "http://" + result.getProperty(2).toString()+ "'");
            System.out.println("HERE IS THE NEW URL " + "http://" + result.getProperty(2).toString());

            contentScreen co = new contentScreen();
            co.signAuto(true);

            Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setClass(this, contentScreen.class);
                startActivity(intent);
                finish();
        }


             catch (Exception e1) {
                // TODO Auto-generated catch block
                 System.out.println("Problem "+ e1.getMessage());
                e1.printStackTrace();
            }
            finally
            {
                sqlDB.close();

            }

    } else {
        username.setTextSize(10);
        username.setText("Invalid username or passcode!");
    }
}


public void getValues()
{
    try{
    sqlDB= this.openOrCreateDatabase(DBNAME2,MODE_PRIVATE,null);
    Cursor r=sqlDB.rawQuery("SELECT Signauto,SaveP FROM "+Table2, null);
    System.out.println("COUNT : " + r.getCount());
    Integer sign=r.getColumnIndex("Signauto");
    Integer save=r.getColumnIndex("SaveP");
    int j=0;
    if(r.moveToFirst()){
    do
    {
        System.out.println("sign values are"+r.getString(sign));
        System.out.println("save values are"+r.getString(save));

        //int a[]=new int[tickBoxes.length];
        j++;

            if(r.getString(sign).equals("true"))
            {
                tickBoxes[0]=true;
                System.out.println("values of tickbox"+tickBoxes[0]);
            }
            else
            {
                tickBoxes[0]=false;
            }
            if(r.getString(save).equals("true"))
            {
                tickBoxes[1]=true;
                System.out.println("values of tickbox"+tickBoxes[1]);
            }
            else
            {
                tickBoxes[1]=false;
            }

    }
    while(r.moveToNext());
    if(j==0)
    {
        System.out.println("No data found");
    }

    }
    r.close();
    //sqlDB.close();
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
    finally
    {
        sqlDB.close();
    }
}

public String[] setUserPass()
{
    try{
        sqlDB= this.openOrCreateDatabase(DBNAME1,MODE_PRIVATE,null);
        Cursor r=sqlDB.rawQuery("SELECT NAME, PASSWORD, URL FROM " +Table1, null);
        System.out.println("COUNT : " + r.getCount());
        Integer uname=r.getColumnIndex("NAME");
        Integer pwd=r.getColumnIndex("PASSWORD");
        Integer url=r.getColumnIndex("URL");

        if(r.moveToFirst())
        {
            do{
        remembered[1]=r.getString(uname);
        System.out.println("remembered 1"+remembered[1]);
        remembered[2]=r.getString(pwd);
        remembered[0]=r.getString(url);
        System.out.println("newnewnew " + r.getString(url));
            }
            while(r.moveToNext());
        }
        r.close();
    }
    catch(Exception e)
    {
        e.printStackTrace();
        System.out.println("BBBBBBBBBBBBBBBBBBBB" + e.toString());
    }
    finally{
        sqlDB.close();
    }
    return remembered;

}

public String[] obtainDetails(){
    try{
        sqlDB= this.openOrCreateDatabase(DBNAME1,MODE_PRIVATE,null);
        Cursor r=sqlDB.rawQuery("SELECT NAME, PASSWORD, URL FROM " +Table1, null);
        System.out.println("COUNT : " + r.getCount());
        Integer uname=r.getColumnIndex("NAME");
        Integer pwd=r.getColumnIndex("PASSWORD");
        Integer url=r.getColumnIndex("URL");
        if(r.moveToFirst())
        {
            do{
        Compare[1]=r.getString(uname);
        System.out.println("remembered 1"+remembered[1]);
        Compare[2]=r.getString(pwd);
        Compare[0]=r.getString(url);
            }
            while(r.moveToNext());
        }
        r.close();
    }
    catch(Exception e)
    {
        e.printStackTrace();
        System.out.println("CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC" + e.toString());
    }
    finally{
        sqlDB.close();
    }
    return Compare;
}

}

2 个答案:

答案 0 :(得分:1)

我认为这是因为setRequestedOrientation:默认情况下,android会在方向更改时重新创建活动。您可以在应用程序清单文件中为活动指定特定方向。

答案 1 :(得分:0)

- 的 _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ 的____ -

我无法相信自己有多愚蠢。我只是看了几个小时的代码并错过了这个。

for (int i = 0; i < tickBoxes.length; i++) {
    if (tickBoxes[1]) {
        saveBox.setChecked(true);

        if(!tickBoxes[0]){

        try {
            //gainString(remembered);

            setUserPass();

            username.setText(remembered[1]);
            System.out.println(remembered[1]);
            password.setText(remembered[2]);

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        }

        // SET THE TEXT HERE IF NEEDED
    }
    if (tickBoxes[0]) {
        checkBox.setChecked(true);
        try {
            autoSign = true;
            System.out.println(autoSign);
            autoGO(arg3);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

条件为真,并且处于for循环中,它会运行两次。这是一个小学生的错误 - 好的-_-