gpt4 book ai didi

java - 无法从 ImageView 类型对非静态方法 setImageBitmap(Bitmap) 进行静态引用

转载 作者:行者123 更新时间:2023-12-01 18:40:01 25 4
gpt4 key购买 nike

问题:

我在 Eclipse 中收到错误,指出“无法从 ImageView 类型对非静态方法 setImageBitmap(Bitmap) 进行静态引用”

来源:

public class Home extends YouTubeBaseActivity implements
YouTubePlayer.OnInitializedListener,
YouTubeThumbnailView.OnInitializedListener {

public static final String API_KEY = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
public static final String VIDEO_ID = "o7VVHhK9zf0";
public static final String VIDEO1_ID = "xVHHJqntuXI";
public static final String VIDEO2_ID = "YWteQj_q3Ro";
public static final String VIDEO3_ID = "83ZgtqTw-mI";
public static final String VIDEO4_ID = "n5wMza29JwI";
private YouTubePlayer youTubePlayer;
private YouTubePlayerView youTubePlayerView;
private YouTubeThumbnailView youTubeThumbnailView1;
private YouTubeThumbnailView youTubeThumbnailView2;
private YouTubeThumbnailView youTubeThumbnailView3;
private YouTubeThumbnailView youTubeThumbnailView4;
private String[] drawerListViewItems;
private DrawerLayout drawerLayout;
private DrawerLayout mDrawerLayout;
private ListView drawerListView;
private ListView mDrawerList;
private ActionBarDrawerToggle actionBarDrawerToggle;
private ActionBarDrawerToggle mDrawerToggle;
Drawable selstation_up_btn;
private YouTubeThumbnailLoader youTubeThumbnailLoader;
ScrollView mainScrollView;
Button fav_up_btn1;
Button fav_dwn_btn1;
ArrayAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);

ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
ImagePagerAdapter adapter = new ImagePagerAdapter();
viewPager.setAdapter(adapter);

final ActionBar actionBar = getActionBar();
actionBar.setCustomView(R.layout.actionbar_custom_view_home);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);


Drawable d = getResources().getDrawable(selstation_up_btn);
imageView.setImageBitmap(((BitmapDrawable) d).getBitmap());

引文/示例来源/初始问题:

Footer Image Will Not Size Correctly

编辑(回应拉马拉尔)

public class Home extends YouTubeBaseActivity implements
YouTubePlayer.OnInitializedListener,
YouTubeThumbnailView.OnInitializedListener {

public static final String API_KEY = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
public static final String VIDEO_ID = "o7VVHhK9zf0";
public static final String VIDEO1_ID = "xVHHJqntuXI";
public static final String VIDEO2_ID = "YWteQj_q3Ro";
public static final String VIDEO3_ID = "83ZgtqTw-mI";
public static final String VIDEO4_ID = "n5wMza29JwI";
private YouTubePlayer youTubePlayer;
private YouTubePlayerView youTubePlayerView;
private YouTubeThumbnailView youTubeThumbnailView1;
private YouTubeThumbnailView youTubeThumbnailView2;
private YouTubeThumbnailView youTubeThumbnailView3;
private YouTubeThumbnailView youTubeThumbnailView4;
private String[] drawerListViewItems;
private DrawerLayout drawerLayout;
private DrawerLayout mDrawerLayout;
private ListView drawerListView;
private ListView mDrawerList;
private ActionBarDrawerToggle actionBarDrawerToggle;
private ActionBarDrawerToggle mDrawerToggle;
Drawable selstation_up_btn;
ImageView imageView;
private YouTubeThumbnailLoader youTubeThumbnailLoader;
ScrollView mainScrollView;
Button fav_up_btn1;
Button fav_dwn_btn1;
ArrayAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);

ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
ImagePagerAdapter adapter = new ImagePagerAdapter();
viewPager.setAdapter(adapter);

final ActionBar actionBar = getActionBar();
actionBar.setCustomView(R.layout.actionbar_custom_view_home);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);


ImageView myImageView = (ImageView) findViewById(R.id.selstation_up_btn);
Drawable d = getResources().getDrawable(R.drawable.selstation_up_btn);
imageView.setImageDrawable(d);

编辑后的问题(回应拉马拉尔)

12-02 10:28:23.619: E/AndroidRuntime(24278): FATAL EXCEPTION: main
12-02 10:28:23.619: E/AndroidRuntime(24278): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.idg2/com.idg.omv.Home}: java.lang.NullPointerException
12-02 10:28:23.619: E/AndroidRuntime(24278): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
12-02 10:28:23.619: E/AndroidRuntime(24278): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
12-02 10:28:23.619: E/AndroidRuntime(24278): at android.app.ActivityThread.access$700(ActivityThread.java:143)
12-02 10:28:23.619: E/AndroidRuntime(24278): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
12-02 10:28:23.619: E/AndroidRuntime(24278): at android.os.Handler.dispatchMessage(Handler.java:99)
12-02 10:28:23.619: E/AndroidRuntime(24278): at android.os.Looper.loop(Looper.java:137)
12-02 10:28:23.619: E/AndroidRuntime(24278): at android.app.ActivityThread.main(ActivityThread.java:4950)
12-02 10:28:23.619: E/AndroidRuntime(24278): at java.lang.reflect.Method.invokeNative(Native Method)
12-02 10:28:23.619: E/AndroidRuntime(24278): at java.lang.reflect.Method.invoke(Method.java:511)
12-02 10:28:23.619: E/AndroidRuntime(24278): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
12-02 10:28:23.619: E/AndroidRuntime(24278): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
12-02 10:28:23.619: E/AndroidRuntime(24278): at dalvik.system.NativeStart.main(Native Method)
12-02 10:28:23.619: E/AndroidRuntime(24278): Caused by: java.lang.NullPointerException
12-02 10:28:23.619: E/AndroidRuntime(24278): at com.idg.omv.Home.onCreate(Home.java:89)
12-02 10:28:23.619: E/AndroidRuntime(24278): at android.app.Activity.performCreate(Activity.java:5179)
12-02 10:28:23.619: E/AndroidRuntime(24278): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
12-02 10:28:23.619: E/AndroidRuntime(24278): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
12-02 10:28:23.619: E/AndroidRuntime(24278): ... 11 more

最佳答案

这是因为 setImageBitmap 似乎不是一个静态方法,但你可以像调用它一样。您可能需要一个 ImageView 实例并在该实例上调用 setImageBitmap

即您需要从 View 中获取 ImageView 的实例并调用然后调用该方法。

@Override
protected void onCreate(Bundle savedInstanceState) {
...
ImageView imgView = (ImageView) findViewById(R.id.the_id);
imgView.setImageBitmap(((BitmapDrawable) d).getBitmap());
}

您还可以使用setImageDrawable可能更适合您的情况的方法。

@Override
protected void onCreate(Bundle savedInstanceState) {
...
ImageView imgView = (ImageView) findViewById(R.id.the_id);
imgView.setImageDrawable(d);
}

由于您的错误发生是因为可绘制对象为空,请尝试更改此设置

Drawable d = getResources().getDrawable(R.drawable.selstation_up_btn);
ImageView imgView = (ImageView) findViewById(R.id.the_id);
imgView.setImageBitmap(((BitmapDrawable) d).getBitmap());

到此

Bitmap d = BitmapFactory.decodeResource(context.getResources(),
R.drawable.selstation_up_btn);
ImageView imgView = (ImageView) findViewById(R.id.the_id);
imgView.setImageBitmap(d);

关于java - 无法从 ImageView 类型对非静态方法 setImageBitmap(Bitmap) 进行静态引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20331063/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com