Android 如何使用toast功能?

Android 如何使用toast功能?,android,syntax-error,Android,Syntax Error,我刚开始学习Android Studio。我想通过按下小部件上的按钮来显示toast通知。但是,使用getApplicationContext()函数时出错。你能告诉我怎么修吗?其他方面,包括getBaseContext(),都是一样的。。。。。错误消息是“无法解析方法”getApplicationContext()“”我不会说英语,所以我使用了谷歌翻译。。。。如果你不明白,我很抱歉 package com.jhjsapp.widget; import android.appwidget.Ap

我刚开始学习Android Studio。我想通过按下小部件上的按钮来显示toast通知。但是,使用getApplicationContext()函数时出错。你能告诉我怎么修吗?其他方面,包括getBaseContext(),都是一样的。。。。。错误消息是“无法解析方法”getApplicationContext()“”我不会说英语,所以我使用了谷歌翻译。。。。如果你不明白,我很抱歉

package com.jhjsapp.widget;

import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.view.View;
import android.widget.RemoteViews;
import android.widget.Toast;

/**
 * Implementation of App Widget functionality.
 */
public class TestWidget extends AppWidgetProvider {

    static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
                                int appWidgetId) {

        CharSequence widgetText = context.getString(R.string.appwidget_text);
        // Construct the RemoteViews object
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.test_widget);
        views.setTextViewText(R.id.appwidget_text, widgetText);

        // Instruct the widget manager to update the widget
        appWidgetManager.updateAppWidget(appWidgetId, views);
    }

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        // There may be multiple widgets active, so update all of them
        for (int appWidgetId : appWidgetIds) {
            updateAppWidget(context, appWidgetManager, appWidgetId);
        }
    }

    @Override
    public void onEnabled(Context context) {
        // Enter relevant functionality for when the first widget is created
    }

    @Override
    public void onDisabled(Context context) {
        // Enter relevant functionality for when the last widget is disabled
    }

    public void butcli(View view){
        Toast.makeText(getApplicationContext(),"버튼 눌림", Toast.LENGTH_SHORT).show();
    }
}
这是JAVA代码


无法使用
getApplicationContext()
显示
Toast
消息,因为小部件扩展了AppWidgetProvider。因此,无法获取当前上下文

您可以尝试以下方法:

public class testWidget extends AppWidgetProvider{

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {

final int N = appWidgetIds.length;

for (int i=0; i<N; i++) {
    int appWidgetId = appWidgetIds[i];

    Intent intent = new Intent(context, MainPage.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widgetlayout);
    views.setOnClickPendingIntent(R.id.button1, pendingIntent);

    appWidgetManager.updateAppWidget(appWidgetId, views);
   }
}

@Override
public void onReceive(Context context, Intent intent) {   
    super.onReceive(context, intent);   
    Toast.makeText(context, "Button Clicked", Toast.LENGTH_SHORT).show();
}
公共类testWidget扩展了AppWidgetProvider{
@凌驾
公共void onUpdate(上下文上下文,AppWidgetManager AppWidgetManager,int[]AppWidgetId){
final int N=appWidgetIds.length;

对于(int i=0;i首先创建一个全局变量

private Context context;
然后通过将此行添加到updateAppWidget()和onUpdate()以及onEnabled()和onDisabled()来初始化上下文

然后使用吐司,如:

Toast.makeText(context,"버튼 눌림", Toast.LENGTH_SHORT).show();

你确定要构建一个
AppWidget
?为什么不从更基本的东西开始呢?谢谢你们的帮助。多亏了你们,我已经能够修复它了!!
Toast.makeText(context,"버튼 눌림", Toast.LENGTH_SHORT).show();