Cannot find symbol 'Context', android.content.Context

前端 未结 2 1946
囚心锁ツ
囚心锁ツ 2021-01-18 03:21

I have the following code:

package com.androidtest.notification;

import android.app.Activity;
import android.os.Bundle;
import android.widget;
import androi         


        
相关标签:
2条回答
  • 2021-01-18 03:34

    Context in Activity is obtained by YourActivity.this or easier with this

    package com.androidtest.notification;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.widget;
    import android.widget.Toast;
    import android.content.Context;
    
    public class ActivityNotification extends Activity
    {
    
        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            Context context = this; // or ActivityNotification.this
            CharSequence text = "Hello toast!";
            int duration = Toast.LENGTH_SHORT;
    
            Toast toast = Toast.makeText(this, text, duration);
            toast.show();
        }
    }
    
    0 讨论(0)
  • 2021-01-18 03:43

    Short answer: add this

    import android.content.Context;
    
    0 讨论(0)
提交回复
热议问题