绕过错误'无法解析符号'

时间:2016-02-15 05:55:24

标签: java android anonymous-class anonymous-methods android-toast

  

如何解决这个问题?

我想在使用onclick点击imageview时显示toast  监听器。

main activity.java:

public class LoginActivity extends AppCompatActivity implements LoaderCallbacks<Cursor> {

    /**
     * Id to identity READ_CONTACTS permission request.
     */
    private static final int REQUEST_READ_CONTACTS = 0;

    /**
     * A dummy authentication store containing known user names and passwords.
     * TODO: remove after connecting to a real authentication system.
     */
    private static final String[] DUMMY_CREDENTIALS = new String[]{
            "foo@example.com:hello", "bar@example.com:world"
    };
    /**
     * Keep track of the login task to ensure we can cancel it if requested.
     */
    private UserLoginTask mAuthTask = null;
    ObjectAnimator objectAnimator=new ObjectAnimator();

    // UI references.
    public AutoCompleteTextView mEmailView;
    public EditText mPasswordView;
    private View mProgressView;
    private View mLoginFormView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        FacebookSdk.sdkInitialize(this.getApplicationContext());
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        // Set up the login form.
        mEmailView = (AutoCompleteTextView) findViewById(R.id.email);
        populateAutoComplete();
        ImageView mImageView= (ImageView) findViewById(R.id.imageView);

        mPasswordView = (EditText) findViewById(R.id.password);
        mPasswordView.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int id, KeyEvent keyEvent) {
                if (id == R.id.login || id == EditorInfo.IME_NULL) {
                    attemptLogin();
                    return true;
                }
                return false;
            }
        });

        Button mEmailSignInButton = (Button) findViewById(R.id.email_sign_in_button);
        mEmailSignInButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                attemptLogin();
            }
        });

        mLoginFormView = findViewById(R.id.login_form);
        mProgressView = findViewById(R.id.login_progress);

       Profile profile = Profile.getCurrentProfile().getCurrentProfile();
        if (profile != null) {
            // user has logged in

        } else {
            // user has not logged in

        }
       // SpellCheckerService.Session;//.openActiveSession(this, true, new SpellCheckerService.Session.StatusCallback() {
            // callback when session changes state
         //   @Override
         //   public void call(SpellCheckerService.Session session, SessionState state, Exception exception) {
          //  }
      //  });


        mImageView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                Toast.makeText(this,"done",Toast.LENGTH_SHORT).show();
            }

        });

    }

错误在这里:

  

mImageView.setOnClickListener(new View.OnClickListener(){

     

@覆盖   public void onClick(查看视图){                     Toast.makeText(这一点, “完成”,Toast.LENGTH_SHORT).show();   }

     

});

无法解析方法'maketext(anonymous ....)'

4 个答案:

答案 0 :(得分:1)

使用LoginActivity.this代替this作为makeText的第一个参数

 mImageView.setOnClickListener(new View.OnClickListener() {

    @Override public void onClick(View view) {
           Toast.makeText(LoginActivity.this,"done",Toast.LENGTH_SHORT).show(); }
    });

希望它会有所帮助!

答案 1 :(得分:1)

您需要使用Activity context而不是"this"例如

 Toast.makeText(MainActivity.this, "Your message", Toast.LENGTH_LONG).show();

您当前正在调用该按钮的上下文。

答案 2 :(得分:0)

Change 
Toast.makeText(this,"done",Toast.LENGTH_SHORT).show();
to
Toast.makeText(getApplicationContext(),"done",Toast.LENGTH_SHORT).show();
or 
Toast.makeText(LoginActivity.this,"done",Toast.LENGTH_SHORT).show();

答案 3 :(得分:0)

尝试使用

 Toast.makeText(LoginActivity.this, "Done", Toast.LENGTH_LONG).show();