【问题标题】:Cannot be resolved to a type error无法解决类型错误
【发布时间】:2013-12-29 20:51:04
【问题描述】:

我收到“无法将注册解析为我的代码中的类型错误,我不知道为什么。我有强烈的感觉这是一个 Eclipse 错误,除非有人能看到其他东西。我只是随机得到这个编辑另一个页面时出错,所以我不知道为什么会这样。

我尝试清理项目,甚至重建项目,仍然出现此错误。

这是Eclipse中的错误截图:

更奇怪的是,我在输入代码时尝试查看可用的类,而 Registration.java 没有出现在可用列表中:

关于如何让它显示在该列表中的任何想法?

这是我的代码:

package com.smeet;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
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 com.sencide.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;



public class AndroidLogin extends Activity implements OnClickListener {


    Button ok,back,exit;
    TextView result;




    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
       setContentView(R.layout.main);



        // Login button clicked
        ok = (Button)findViewById(R.id.btn_login);
        ok.setOnClickListener(this);


        result = (TextView)findViewById(R.id.tbl_result);



    }









    public void postLoginData() {
        // Create a new HttpClient and Post Header
        HttpClient httpclient = new DefaultHttpClient();


        // login.php returns true if username and password match in db 
        HttpPost httppost = new HttpPost("http://www.somewebsite.com/android/login.php");

        try {




            // Add user name and password
            EditText uname = (EditText)findViewById(R.id.txt_username);
            String username = uname.getText().toString();



            EditText pword = (EditText)findViewById(R.id.txt_password);
            String password = pword.getText().toString();

            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            nameValuePairs.add(new BasicNameValuePair("username", username));
            nameValuePairs.add(new BasicNameValuePair("password", password));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            // Execute HTTP Post Request
            Log.w("SENCIDE", "Execute HTTP Post Request");
            HttpResponse response = httpclient.execute(httppost);

            String str = inputStreamToString(response.getEntity().getContent()).toString();
            Log.w("SENCIDE", str);

            if(str.toString().equalsIgnoreCase("true"))
            {
                Log.w("SENCIDE", "TRUE");
                result.setText("Login Successful! Please Wait...");   
            }else
            {
                Log.w("SENCIDE", "FALSE");
                result.setText(str);                
            }

        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    } 

    private StringBuilder inputStreamToString(InputStream is) {
        String line = "";
        StringBuilder total = new StringBuilder();
        // Wrap a BufferedReader around the InputStream
        BufferedReader rd = new BufferedReader(new InputStreamReader(is));
        // Read response until the end
        try {
            while ((line = rd.readLine()) != null) { 
                total.append(line); 
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        // Return full string
        return total;
    }


    //clicking register button will start up Register xml
    public void RegisterButton(View view) {

          Intent intent = new Intent(this, Registration.class);
          startActivity(intent);

    }




    @Override
    public void onClick(View view) {
                postLoginData();




                // turns the text in the textview "Tbl_result" into a text string called "tblresult"
                TextView tblresult = (TextView) findViewById(R.id.tbl_result);




        // If "tblresult" text string matches the string "Login Successful! Please Wait..." exactly, it will switch to next activity
                if (tblresult.getText().toString().equals("Login Successful! Please Wait...")) {
                      Intent intent = new Intent(this, Homepage.class);
                      EditText uname2 = (EditText)findViewById(R.id.txt_username);
                      String username2 = uname2.getText().toString();
                      intent.putExtra("username2", username2);
                      startActivity(intent);
                   }    






    }

}

我的清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.sencide"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:icon="@drawable/icon"
        android:label="@string/app_name" >
        <activity
            android:name="com.sencide.AndroidLogin"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.sencide.NextActivity"
            android:label="@string/title_activity_next" >
        </activity>
           <activity
            android:name="com.sencide.Registration"
            android:label="@string/title_activity_registration" >
        </activity>
        <activity
            android:name="com.sencide.Homepage"
            android:label="@string/title_activity_next_screen" >
        </activity>


    </application>

</manifest>

这是我的Registration.java

package com.smeet;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import com.sencide.R;

import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;


class RequestTask extends AsyncTask<String, String, String>{
      String responseString = null;


    @Override
    protected String doInBackground(String... uri) {
        HttpClient httpclient = new DefaultHttpClient();
        HttpResponse response;

        try {
            response = httpclient.execute(new HttpGet(uri[0]));
            StatusLine statusLine = response.getStatusLine();
            if(statusLine.getStatusCode() == HttpStatus.SC_OK){
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                response.getEntity().writeTo(out);
                out.close();
                String responseString = null;
                responseString = out.toString();
                Log.d("check response", responseString);


            } else{
                //Closes the connection.
                response.getEntity().getContent().close();
                throw new IOException(statusLine.getReasonPhrase());
            }
        } catch (ClientProtocolException e) {
            //TODO Handle problems..
        } catch (IOException e) {
            //TODO Handle problems..
        }
        return responseString;
    }


    protected void onPostExecute(String result) {
        super.onPostExecute(result);


    }







String result = null;

public class Registration extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);
          TextView detail = (TextView)findViewById(R.id.resulttext);
            detail.setText(result);
    }


    public void SubmitRegistration(View view) {




           // assign text in fields to string values
           EditText first = (EditText)findViewById(R.id.first);
           String first2 = first.getText().toString();

           EditText last = (EditText)findViewById(R.id.last);
           String last2 = last.getText().toString();

           EditText display = (EditText)findViewById(R.id.display);
           String display2 = display.getText().toString();
           //calculates the number of characters in the display field
           int length2 = display2.length();


           EditText email = (EditText)findViewById(R.id.email);
           String email2 = email.getText().toString();


           EditText password = (EditText)findViewById(R.id.password);
           String password2 = password.getText().toString();


           EditText vpassword = (EditText)findViewById(R.id.vpassword);
           String vpassword2 = vpassword.getText().toString();
           //calculates the number of characters in the password field
           int length = vpassword2.length();






    // verifying the following in order:  Passwords match? A Password field is empty?  
                                        //Password and Display Name less than 6 characters long? Email contains an @ sign and a period?   
           if(!vpassword2.equals(password2))
           {
               Toast.makeText(getApplicationContext(), "Passwords do not match!", Toast.LENGTH_SHORT).show(); 


           }
           else if (password2.isEmpty() || vpassword2.isEmpty()){

               Toast.makeText(getApplicationContext(), "Password field is empty", Toast.LENGTH_SHORT).show(); 
           }
           else if (length < 6 || length2 < 6 ) {

               Toast.makeText(getApplicationContext(), "Password and Display Name must be at least 6 characters long", Toast.LENGTH_LONG).show(); 

           }

           else if (!email2.contains("@") || !email2.contains(".")){

               Toast.makeText(getApplicationContext(), "Must enter valid email address.", Toast.LENGTH_SHORT).show(); 

           }

           else {


               //send php with all the data to server for validation and insertion into table
               new RequestTask().execute("http://www.somewebsite.com/android/registercheck.php?first=" + first2 + "&last=" + last2 + "&dispname=" + display2 + "&email=" + email2 + "&password=" + password2 );                       



           }
   }







}

}

【问题讨论】:

  • Registration 类型声明在哪里?
  • 注册类型是如何声明的?
  • 不应该导入课程吗?
  • 不确定你的意思。这是一堂课。它在清单中。
  • 在清单中是否明确定义。再次检查。

标签: java android eclipse class


【解决方案1】:

根据您帖子中的 cmets 和图像,我怀疑您在 Registration.java 中有

public class Register ...

什么时候应该有

public class Registration ...

另外,既然您已经添加了代码,Registration 类应该是独立的,而不是嵌套在 RequestTask 中。

【讨论】:

  • 所以你一开始是对的,但即使我将其更改为注册,我也得到了同样的错误。 :( 我为我的 Registration.java 发布了我的代码
【解决方案2】:

Registration 类位于您的GCM backend module 中。如果您使用 Android Studio,则集成是无缝的,但在 Eclipse 中,您必须手动执行一些技巧才能使其正常工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-26
    • 2022-07-09
    • 1970-01-01
    • 1970-01-01
    • 2017-05-01
    • 2020-02-01
    • 1970-01-01
    相关资源
    最近更新 更多