嗨我希望根据屏幕分辨率更改图像的高度和宽度,所以我使用下面的代码,但我在logcat中收到错误:
我的第一个源代码:
public class ImageviewAppActivity extends Activity {
private ImageView img;
private LayoutParams params;
private int width, height;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
height=160; //getWindowManager().getDefaultDisplay().getHeight();
width = 160; //getWindowManager().getDefaultDisplay().getWidth();
params = new LayoutParams(width, height);
img.setLayoutParams(params);
img =(ImageView) findViewById(R.id.imagetest);
img.setImageResource(R.drawable.last);
}}
logcat错误:
模拟器错误:
05-19 20:02:08.689: D/AndroidRuntime(1933): Shutting down VM
05-19 20:02:08.689: W/dalvikvm(1933): threadid=1: thread exiting with uncaught exception (group=0x40015560)
05-19 20:02:08.749: E/AndroidRuntime(1933): FATAL EXCEPTION: main
05-19 20:02:08.749: E/AndroidRuntime(1933): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test/com.test.ImageviewAppActivity}: java.lang.NullPointerException
05-19 20:02:08.749: E/AndroidRuntime(1933): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
05-19 20:02:08.749: E/AndroidRuntime(1933): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
05-19 20:02:08.749: E/AndroidRuntime(1933): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-19 20:02:08.749: E/AndroidRuntime(1933): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
05-19 20:02:08.749: E/AndroidRuntime(1933): at android.os.Handler.dispatchMessage(Handler.java:99)
05-19 20:02:08.749: E/AndroidRuntime(1933): at android.os.Looper.loop(Looper.java:123)
05-19 20:02:08.749: E/AndroidRuntime(1933): at android.app.ActivityThread.main(ActivityThread.java:3683)
05-19 20:02:08.749: E/AndroidRuntime(1933): at java.lang.reflect.Method.invokeNative(Native Method)
05-19 20:02:08.749: E/AndroidRuntime(1933): at java.lang.reflect.Method.invoke(Method.java:507)
05-19 20:02:08.749: E/AndroidRuntime(1933): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-19 20:02:08.749: E/AndroidRuntime(1933): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-19 20:02:08.749: E/AndroidRuntime(1933): at dalvik.system.NativeStart.main(Native Method)
05-19 20:02:08.749: E/AndroidRuntime(1933): Caused by: java.lang.NullPointerException
05-19 20:02:08.749: E/AndroidRuntime(1933): at com.test.ImageviewAppActivity.onCreate(ImageviewAppActivity.java:46)
05-19 20:02:08.749: E/AndroidRuntime(1933): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-19 20:02:08.749: E/AndroidRuntime(1933): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
05-19 20:02:08.749: E/AndroidRuntime(1933): ... 11 more
05-19 20:02:08.779: W/ActivityManager(75): Force finishing activity com.test/.ImageviewAppActivity
05-19 20:02:09.329: W/ActivityManager(75): Activity pause timeout for HistoryRecord{40970fc8 com.test/.ImageviewAppActivity}
05-19 20:02:17.199: D/dalvikvm(378): GC_EXPLICIT freed 7K, 54% free 2537K/5511K, external 1625K/2137K, paused 171ms
05-19 20:02:21.880: W/ActivityManager(75): Activity destroy timeout for HistoryRecord{40970fc8 com.test/.ImageviewAppActivity}
应用程序ImageviewApp(进程com.test.app)意外停止。请再试一次。
答案 0 :(得分:2)
img.setLayoutParams(params);
在此行之后放上行
img =(ImageView) findViewById(R.id.imagetest);
和强>
你的LayoutParams
问题,更改它检查导入包的layourparams并更改它...我不知道你有哪个视图组...但检查下面的包使用一个......
import android.widget.RelativeLayout.LayoutParams;
或强>
import android.view.ViewGroup.LayoutParams;