Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
617 views
in Technique[技术] by (71.8m points)

android - StartActivity() red highlighted

Basically I want to have a button to start a new activity after login. I found that I was not able to call the StartActivity() as what I did before in the login page.Please guide

This is the login page where I used StartActivity(this,sth.class)sucessfully

public class Login extends Activity
{

/** Called when the activity is first created. */


Button login;
String name="",pass="";
EditText username,password;
TextView tv;
byte[] data;
HttpPost httppost;
StringBuffer buffer;
HttpResponse response;
HttpClient httpclient;
InputStream inputStream;
SharedPreferences app_preferences ;
List<NameValuePair> nameValuePairs;
CheckBox check;


public void onCreate(Bundle savedInstanceState)

{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    app_preferences = PreferenceManager.getDefaultSharedPreferences(this);

    username = (EditText) findViewById(R.id.username);
    password = (EditText) findViewById(R.id.password);
    login = (Button) findViewById(R.id.login);
    check = (CheckBox) findViewById(R.id.check);

     String Str_user = app_preferences.getString("username","0" );
    String Str_pass = app_preferences.getString("password", "0");

    String Str_check = app_preferences.getString("checked", "no");
    if(Str_check.equals("yes"))

    {
            username.setText(Str_user);
            password.setText(Str_pass);
            check.setChecked(true);
    }

    login.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View v)

        {

            name = username.getText().toString();

            pass = password.getText().toString();

            String Str_check2 = app_preferences.getString("checked", "no");

            if(Str_check2.equals("yes"))

            {

                SharedPreferences.Editor editor = app_preferences.edit();

                editor.putString("username", name);

                editor.putString("password", pass);

                 editor.commit();

            }

            if(name.equals("") || pass.equals(""))

            {

                 Toast.makeText(Login.this, "Blank Field..Please Enter", Toast.LENGTH_LONG).show();

            }

            else

            {



            try {

                httpclient = new DefaultHttpClient();

                httppost = new HttpPost("http://fyptest.comyr.com/main.php");

                // Add your data

                nameValuePairs = new ArrayList<NameValuePair>(2);

               nameValuePairs.add(new BasicNameValuePair("UserEmail", name.trim()));

                nameValuePairs.add(new BasicNameValuePair("Password", pass.trim()));

                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));



                // Execute HTTP Post Request

                response = httpclient.execute(httppost);

                inputStream = response.getEntity().getContent();



                data = new byte[256];



                buffer = new StringBuffer();

                int len = 0;

                while (-1 != (len = inputStream.read(data)) )

                {

                    buffer.append(new String(data, 0, len));

                }


                inputStream.close();

            }



            catch (Exception e)

            {

                Toast.makeText(Login.this, "error"+e.toString(), Toast.LENGTH_LONG).show();

            }

            if(buffer.charAt(0)=='Y')

            {

                Toast.makeText(Login.this, "login successfull", Toast.LENGTH_LONG).show();
                Move_to_next();

            }

            else
        {

                Toast.makeText(Login.this, "Invalid Username or password", Toast.LENGTH_LONG).show();

            }

            }

        }

    });

    check.setOnClickListener(new View.OnClickListener()

    {

        public void onClick(View v)

        {

            // Perform action on clicks, depending on whether it's now checked

            SharedPreferences.Editor editor = app_preferences.edit();

            if (((CheckBox) v).isChecked())

            {



                 editor.putString("checked", "yes");

                 editor.commit();

            }

            else

            {

                 editor.putString("checked", "no");

                 editor.commit();

            }

        }

    });

}



public void Move_to_next()

    {

     //may perform checking based on ID

      startActivity(new Intent(this, MainMenu.class));

    }

But this, my startActivity is being underlined in red

public class MainMenu extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_menu);

    Button new_folder = (Button)findViewById(R.id.new_folder);
    new_folder.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v) {
            // show another class 
            startActivity(new Intent(this,Folder_Details.class));

        }

    });
}

}

It shows "The constructor Intent(new View.OnClickListener(){}, Class) is undefined" and "Remove arguments to match Intent()" options

I have included the <Activity></Activity> in Manifest. And the code showed above, imports are cut off

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Yes this is because, you are trying to use "this" with refernce to your button instead of your activity. You have to replace it like this,

Instead of startActivity(new Intent(this, Folder_Details.class));

do this,

startActivity(new Intent(MainMenu.this, Folder_Details.class));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...