こんにちは、画面の解像度に応じて画像の高さと幅を変更したいので、以下のコードを使用していますが、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) が予期せず停止しました。もう一度お試しください。