java - 不兼容的类型:Widget 无法转换为 Context
问题描述
我正在开发小部件,我正在尝试将按钮动态添加到我的 LinearLayout。我正在尝试使用此线程中的代码。
Button myButton = new Button(this);
myButton.setText("Push Me");
LinearLayout ll = (LinearLayout)findViewById(R.id.widget_layout);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
ll.addView(myButton, lp);
问题是我收到此错误:
error: incompatible types: Widget cannot be converted to Context
Button myButton = new Button(this);
^
你知道这里有什么问题吗?我的小部件类:
package com.fxteam.malcome;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;
import android.widget.Button;
import android.view.View;
import android.widget.LinearLayout;
import android.util.Log;
public class Widget extends AppWidgetProvider {
private static final String SYNC_CLICKED = "malcome_widget_button";
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
for (int appWidgetId : appWidgetIds) {
Intent intent2 = new Intent(context, MainActivity.class);
intent2.putExtra("accountID", "0");
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent2, 0);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.simple_app_widget);
views.setOnClickPendingIntent(R.id.malcome_widget_button, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, views);
}
Button myButton = new Button(this);
myButton.setText("Push Me");
LinearLayout ll = (LinearLayout)findViewById(R.id.widget_layout);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
ll.addView(myButton, lp);
}
}
所有错误:
BUILD FAILED in 1s
(node:660) UnhandledPromiseRejectionWarning: Error: cmd: Command failed with exit code 1 Error output:
C:\Users\Adam\cordova\widget\malcomeWidget\platforms\android\app\src\main\java\com\fxteam\malcome\Widget.java:47: error: incompatible types: Widget cannot be converted to Context
Button myButton = new Button(this);
^
C:\Users\Adam\cordova\widget\malcomeWidget\platforms\android\app\src\main\java\com\fxteam\malcome\Widget.java:50: error: cannot find symbol
LinearLayout ll = (LinearLayout)findViewById(R.id.widget_layout);
^
symbol: method findViewById(int)
location: class Widget
C:\Users\Adam\cordova\widget\malcomeWidget\platforms\android\app\src\main\java\com\fxteam\malcome\Widget.java:51: error: cannot find symbol
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
^
symbol: class LayoutParams
location: class Widget
C:\Users\Adam\cordova\widget\malcomeWidget\platforms\android\app\src\main\java\com\fxteam\malcome\Widget.java:51: error: cannot find symbol
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
^
symbol: class LayoutParams
location: class Widget
C:\Users\Adam\cordova\widget\malcomeWidget\platforms\android\app\src\main\java\com\fxteam\malcome\Widget.java:51: error: cannot find symbol
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
^
symbol: variable LayoutParams
location: class Widget
C:\Users\Adam\cordova\widget\malcomeWidget\platforms\android\app\src\main\java\com\fxteam\malcome\Widget.java:51: error: cannot find symbol
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
^
symbol: variable LayoutParams
location: class Widget
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: C:\Users\Adam\cordova\widget\malcomeWidget\platforms\android\app\src\main\java\org\apache\cordova\file\AssetFilesystem.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
6 errors
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 1s
at ChildProcess.whenDone (C:\Users\Adam\cordova\widget\malcomeWidget\platforms\android\cordova\node_modules\cordova-common\src\superspawn.js:169:23)
at ChildProcess.emit (events.js:210:5)
at maybeClose (internal/child_process.js:1021:16)
at Process.ChildProcess._handle.onexit (internal/child_process.js:283:5)
(node:660) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:660) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
我为这个问题中的这么多代码道歉,但我希望它尽可能详细。
解决方案
您已通过Widget
实例而不是 Activitycontext
来创建Button
。
利用
Button myButton = new Button(context);
代替
Button myButton = new Button(this);
推荐阅读
- apache-kafka - Kafka / RabbitMQ 中的每条消息确认
- android - file.createNewFile() 的结果;将 SQLite 数据库导出到 CSV Android Java 时被忽略
- c++ - 如果现代opengl中有多个纹理,则没有纹理
- java - 从java中的json数组获取值
- sql - apache derby 复合函数报错问题
- javascript - 删除重复但不删除空元素,并将重复元素存储在另一个数组中
- r - 嵌套 logit:所有嵌套都必须包含至少 2 个项目吗?
- ios - 通过 segue 问题在 UITableView 和 UIViewController 之间推送数据
- python - matplot lib图表的x轴上有太多刻度的奇怪问题
- java - 将 Java 映射返回到不同的类