我在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
答案 0 :(得分:2)
这是因为setImageBitmap
似乎不是一个静态方法,但你称它为一个。您可能需要ImageView
的实例并在该实例上调用setImageBitmap
。
即。你需要从你的视图中获取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为null,请尝试更改此
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);
答案 1 :(得分:1)
您的setImageBitmap()
方法不是static
,但您试图以静态方式调用它。这就是问题所在。
尝试
reference.setImageBitmap(((BitmapDrawable) d).getBitmap()); // if you have a reference to it
或
setImageBitmap(((BitmapDrawable) d).getBitmap()); // if it is in your current class
答案 2 :(得分:1)
如果你的ImageView在R.layout.home 试试这个
ImageView myImageView = (ImageView) findViewById(R.id.your_image_id);
myImageView.setImageBitmap(((BitmapDrawable) d).getBitmap());
修改
imageView = (ImageView) findViewById(R.id.your_image_id);
Drawable d = getResources().getDrawable(R.drawable.selstation_up_btn);
imageView.setImageDrawable(d);
并且selstation_up_btn
必须位于可绘制文件夹中。