0

我使用了一些示例代码并尝试使用用户名和密码为 android 应用程序构建登录屏幕。但是,我收到以下错误"XYZ cannot be resolved or is not a field"

在我的程序中,文件XYZ's中的 :tvbtn_ok字段,以及文件loginerror.java中的 , 。btn_sign_intxt_usernametxt_passwordloginactivity.java

这是我的代码loginerror.java...

    public class LoginError extends Activity {
          Button button;

          public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                System.gc();
                setContentView(R.layout.LoginError);

                TextView textview = (TextView) findViewById(R.id.tv);
                String loginMessage = getIntent().getStringExtra("LoginMessage");
                textview.setText(loginMessage);

                button = (Button) findViewById(R.id.btn_ok);
                button.setOnClickListener(new View.OnClickListener() {
                      public void onClick(View v) {
                            finish();
                      }
                });
          }
    }>

这是我的代码loginactivity.java...

    import javax.xml.parsers.SAXParser;
    import javax.xml.parsers.SAXParserFactory;

    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.client.entity.UrlEncodedFormEntity;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.apache.http.message.BasicNameValuePair;
    import org.apache.http.protocol.HTTP;
    import org.xml.sax.InputSource;
    import org.xml.sax.XMLReader;

    public class LoginActivity extends Activity {
          /** Called when the activity is first created. */
          private static final String TAG = "Login";
          Button signin;
          String loginmessage = null;
          Thread t;
          private SharedPreferences mPreferences; 
          ProgressDialog dialog;

          public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
                mPreferences = getSharedPreferences("CurrentUser", MODE_PRIVATE); 
                if (!checkLoginInfo()) { 
                      signin = (Button) findViewById(R.id.btn_sign_in);
                      signin.setOnClickListener(new OnClickListener() {
                            public void onClick(View v) {
                                  showDialog(0);
                                  t=new Thread() {
                                        public void run() {
                                              tryLogin();
                                        }
                                  };
                            t.start(); 
                            }
                      }); 
                }
                else {
                      /*Directly opens the Welcome page, if the username and password is already available 
                      in the SharedPreferences*/
                      Intent intent=new Intent(getApplicationContext(),Welcome.class);
                      startActivity(intent);
                      finish();
                } 
          }

          protected Dialog onCreateDialog(int id) {
                switch (id) {
                      case 0: {
                            dialog = new ProgressDialog(this);
                            dialog.setMessage("Speek is loading");
                            dialog.setIndeterminate(true);
                            dialog.setCancelable(true);
                            return dialog;
                      } 
                }
                return null;
          }
          private Handler handler = new Handler() {

                public void handleMessage(Message msg) {
                      String loginmsg=(String)msg.obj;
                      if(loginmsg.equals("SUCCESS")) {
                            removeDialog(0);
                            Intent intent=new Intent(getApplicationContext(),Welcome.class);
                            startActivity(intent);
                            finish(); 
                      }
                }
          }; 
          public void tryLogin() {
                Log.v(TAG, "Trying to Login");
                EditText etxt_user = (EditText) findViewById(R.id.txt_username);
                EditText etxt_pass = (EditText) findViewById(R.id.txt_password);
                String username = etxt_user.getText().toString();
                String password = etxt_pass.getText().toString(); 
                DefaultHttpClient client = new DefaultHttpClient();
                HttpPost httppost = new HttpPost("http://www.speek.com");
                List nvps = new ArrayList();
                nvps.add(new BasicNameValuePair("username", username));
                nvps.add(new BasicNameValuePair("password", password));
                try {
                      UrlEncodedFormEntity p_entity = new UrlEncodedFormEntity(nvps,
    HTTP.UTF_8);
                      httppost.setEntity(p_entity);
                      HttpResponse response = client.execute(httppost);
                      Log.v(TAG, response.getStatusLine().toString());
                      HttpEntity responseEntity = response.getEntity();
                      Log.v(TAG, "Set response to responseEntity");

                      SAXParserFactory spf = SAXParserFactory.newInstance();
                      SAXParser sp = spf.newSAXParser();
                      XMLReader xr = sp.getXMLReader();
                      LoginHandler myLoginHandler = new LoginHandler();
                      xr.setContentHandler(myLoginHandler);
                      xr.parse(retrieveInputStream(responseEntity));
                      ParsedLoginDataSet parsedLoginDataSet = myLoginHandler.getParsedLoginData(); 
                      if (parsedLoginDataSet.getExtractedString().equals("SUCCESS")) {
                            // Store the username and password in SharedPreferences after the successful login
                            SharedPreferences.Editor editor=mPreferences.edit();
                            editor.putString("UserName", username);
                            editor.putString("PassWord", password);
                            editor.commit();
                            Message myMessage=new Message();
                            myMessage.obj="SUCCESS";
                            handler.sendMessage(myMessage); 
                      } else if(parsedLoginDataSet.getExtractedString().equals("ERROR")) { 
                            Intent intent = new Intent(getApplicationContext(), LoginError.class);
                            intent.putExtra("LoginMessage", parsedLoginDataSet.getMessage());
                            startActivity(intent); 
                            removeDialog(0);
                      } 
                } catch (Exception e) 
                { 
                      Intent intent = new Intent(getApplicationContext(), LoginError.class);
                      intent.putExtra("LoginMessage", "Unable to login");
                      startActivity(intent);
                      removeDialog(0);
                } 
          }
          private InputSource retrieveInputStream(HttpEntity httpEntity) {
                InputSource insrc = null;
                try {
                      insrc = new InputSource(httpEntity.getContent());
                } catch (Exception e) {
                }
                return insrc;
          } 
          //Checking whether the username and password has stored already or not
          private final boolean checkLoginInfo() {
                boolean username_set = mPreferences.contains("UserName");
                boolean password_set = mPreferences.contains("PassWord"); 
                if ( username_set || password_set ) {
                      return true;
                } 
                return false;
          } 
    }

这是我的main.xml...

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      xmlns:android="http://schemas.android.com/apk/res/android" >

      <Button
            android:id="@+id/btn_sign_in"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:text="Sign In"
            android:layout_x="103dp"
            android:layout_y="197dp"/> 
      <EditText
            android:id="@+id/txt_username"
            android:layout_width="250dp"
            android:layout_height="wrap_content"
            android:hint="Username"
            android:singleLine="true"
            android:textSize="18sp"
            android:layout_x="40dp"
            android:layout_y="32dp" />
      <EditText
            android:id="@+id/txt_password"
            android:layout_width="250dp"
            android:layout_height="wrap_content"
            android:hint="Password"
            android:singleLine="true"
            android:textSize="18sp"
            android:password="true"
            android:layout_x="40dp"
            android:layout_y="86dp" />
</AbsoluteLayout>

这是我的loginerror.xml档案...

    <?xml version="1.0" encoding="utf-8"?>
    <AbsoluteLayout
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          xmlns:android="http://schemas.android.com/apk/res/android" >
          <TextView 
                android:id="@+id/tv"
                android:layout_width="fill_parent" 
                android:layout_height="wrap_content" 
                android:gravity="center_vertical|center_horizontal"/>
          <Button android:id="@+id/btn_ok" android:layout_width="80dp"
                android:layout_height="wrap_content" android:text="OK"
                android:layout_x="83dp" android:layout_y="60dp" />
    </AbsoluteLayout>

错误发生在这些行...

1.  TextView textview = (TextView) findViewById(R.id.tv);
2.  button = (Button) findViewById(R.id.btn_ok);
3.  signin = (Button) findViewById(R.id.btn_sign_in);
4.  EditText etxt_user = (EditText) findViewById(R.id.txt_username);
    EditText etxt_pass = (EditText) findViewById(R.id.txt_password);

我必须在主程序中添加任何东西来解决这个问题吗?问题是什么意思?

4

2 回答 2

0

您可能使用了错误的 R 导入。如果你使用android.R比你发现你的问题。

如果我理解正确,您的 LoginError.java 文件是否在外部项目中?如果是这样,您将无法使用任何生成的 R 类内容。所以没有布局文件,没有drawable,res文件夹中没有任何东西。

如果您仍想使用外部项目,请查看Android Library Project。这可能会对您有所帮助,因为您可以在那里使用布局和可绘制对象。

于 2012-05-30T14:23:40.610 回答
0

我不认为它可能那么简单,但无论如何:您是否在添加布局后刷新了 Eclipse(项目 - 清洁)中的项目?这在我身上经常发生。此外,即使它与问题无关, AbsoluteLayout 已被弃用,如果您确实需要绝对定位,您可以使用 RelativeLayout 及其 LayoutParams 的绝对值。

于 2012-05-30T14:27:37.917 回答