2013-06-20 179 views
0

我在我的Android应用程序中调用了以下代码。当我在旧的三星Dart上运行它时(api < 13),我得到一个NullPointer异常,如下所述。基于设备屏幕大小的Android调整大小

是否有一个特定的原因代码是为它上面的四行工作,但不是它获得NullPointer的行?

@SuppressLint("NewApi") 
@SuppressWarnings("deprecation") 
public void adjustSize() { 
    int width, height; 

    Display display = getWindowManager().getDefaultDisplay(); 
    if (android.os.Build.VERSION.SDK_INT >= 13) { 
     Point size = new Point(); 
     display.getSize(size); 
     width = size.x; 
     height = size.y; 
    } else { 
     width = display.getWidth(); // deprecated 
     height = display.getHeight(); // deprecated 
    } 

    ImageView counties = (ImageView) findViewById(R.id.btnCounties); 
    ImageView members = (ImageView) findViewById(R.id.btnMembers); 
    ImageView webLink = (ImageView) findViewById(R.id.btnWebLink); 
    ImageView logo = (ImageView) findViewById(R.id.logo); 

    // Calculate image sizes 
    if (height > width) { 
     counties.getLayoutParams().height = (int) (width/2.5); 
     counties.getLayoutParams().width = (int) (width/2.5); 
     members.getLayoutParams().height = (int) (width/2.5); 
     members.getLayoutParams().width = (int) (width/2.5); 

     webLink.getLayoutParams().height = (int) ((width/2.4)/3.5); // Null pointer error 
     webLink.getLayoutParams().width = (int) (width/2.4); 
     logo.getLayoutParams().height = (int) (height/6); 
     logo.getLayoutParams().width = width; 
    } else { 
     counties.getLayoutParams().height = (int) (height/2.5); 
     counties.getLayoutParams().width = (int) (height/2.5); 
     members.getLayoutParams().height = (int) (height/2.5); 
     members.getLayoutParams().width = (int) (height/2.5); 
    } 
} 

回答

1

简单,

ImageView webLink = (ImageView) findViewById(R.id.btnWebLink); 

webLink

检查您的布局xml:您确定名称(btnWebLink)是否正确?你确定你正在加载正确的布局xml文件吗?

Probabily你有一个布局ImageView但Android是加载布局为特定的屏幕尺寸那里没有btnWebLink。

在该行中有断点并检查变量webLink是否为空。

空指针异常来第一次您尝试使用该变量。

+0

谢谢!这正是应用程序的错误。 – mjhouseman

+0

它总是发生在我身上! ;) –

相关问题