【问题标题】:StartActivity() red highlightedStartActivity() 红色突出显示
【发布时间】:2011-11-25 08:24:40
【问题描述】:

基本上我想要一个按钮在登录后开始一个新的活动。 我发现我无法像以前在登录页面中那样调用 StartActivity()。请指导

这是我成功使用 StartActivity(this,sth.class) 的登录页面

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));

    }

但是这个,我的 startActivity 是用红色下划线的

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));

        }

    });
}

}

它显示“构造函数 Intent(new View.OnClickListener(){}, Class) 未定义”和“删除参数以匹配 Intent()”选项

我已将&lt;Activity&gt;&lt;/Activity&gt; 包含在清单中。而上面显示的代码,导入被切断了

【问题讨论】:

    标签: android


    【解决方案1】:

    是的,这是因为您尝试使用“this”来引用您的按钮而不是您的活动。你必须像这样替换它,

    而不是startActivity(new Intent(this, Folder_Details.class));

    这样做,

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

    【讨论】:

      【解决方案2】:

      修改为

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

      您正在使用 View.OnClickListener 而不是 Context 实例。

      【讨论】:

        【解决方案3】:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-09-02
          • 2014-02-15
          • 1970-01-01
          • 2018-06-24
          • 2018-06-08
          • 1970-01-01
          • 2015-04-05
          • 1970-01-01
          相关资源
          最近更新 更多