我一直致力于在我的应用程序中支持多个屏幕。但是,当我的屏幕被检测为 480x800 屏幕时,它会切换到该分辨率的 Activity,但此时它会崩溃...
这是detect.java,它根据屏幕分辨率做出决定:
package com.example.mywallpapers;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Point;
import android.os.Build;
import android.os.Bundle;
import android.view.Display;
import android.view.WindowManager;
public class Detect extends Activity {
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
int width = 0;
Point size = new Point();
WindowManager w = getWindowManager();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
w.getDefaultDisplay().getSize(size);
width = size.x;
} else {
Display d = w.getDefaultDisplay();
width = d.getWidth();
}
if (width == 480) {
startActivity(new Intent(Detect.this, SevenTwentyOne.class));
} else if (width == 720) {
startActivity(new Intent("com.example.mywallpapers.FOUREIGHTONE"));
} else {
startActivity(new Intent("com.example.mywallpapers.FOUREIGHTONE"));
}
}
}
这是我的 SevenTwentyOne 课程(不是针对 1280x720 分辨率,而是针对 480x800)
package com.example.mywallpapers;
import java.io.IOException;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import com.mopub.mobileads.MoPubView;
public class SevenTwentyOne extends Activity {
ImageView display;
int toPhone;
private MoPubView mAdView;
MediaPlayer knopje;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.foureightyeighthundredeen);
mAdView = (MoPubView) findViewById(R.id.adview);
mAdView.setAdUnitId("e95a36beb72611e281c11231392559e4"); // Enter your
// Ad Unit
// ID from
// www.mopub.com
mAdView.loadAd();
display = (ImageView) findViewById(R.id.WaP49);
Button image1 = (Button) findViewById(R.id.WaP50);
Button image2 = (Button) findViewById(R.id.WaP51);
Button image3 = (Button) findViewById(R.id.WaP52);
Button image4 = (Button) findViewById(R.id.WaP53);
Button image5 = (Button) findViewById(R.id.WaP54);
Button image6 = (Button) findViewById(R.id.WaP55);
Button image7 = (Button) findViewById(R.id.WaP56);
Button image8 = (Button) findViewById(R.id.WaP57);
Button image9 = (Button) findViewById(R.id.WaP58);
Button image10 = (Button) findViewById(R.id.WaP59);
Button image11 = (Button) findViewById(R.id.WaP60);
Button image12 = (Button) findViewById(R.id.WaP61);
Button image13 = (Button) findViewById(R.id.WaP62);
Button image14 = (Button) findViewById(R.id.WaP63);
Button image15 = (Button) findViewById(R.id.WaP64);
Button image16 = (Button) findViewById(R.id.WaP65);
Button image17 = (Button) findViewById(R.id.WaP66);
Button image18 = (Button) findViewById(R.id.WaP67);
Button image19 = (Button) findViewById(R.id.WaP68);
Button image20 = (Button) findViewById(R.id.WaP69);
Button image21 = (Button) findViewById(R.id.WaP70);
Button image22 = (Button) findViewById(R.id.WaP71);
Button image23 = (Button) findViewById(R.id.WaP72);
Button image24 = (Button) findViewById(R.id.WaP73);
Button image25 = (Button) findViewById(R.id.WaP74);
Button image26 = (Button) findViewById(R.id.WaP75);
Button image27 = (Button) findViewById(R.id.WaP76);
Button image28 = (Button) findViewById(R.id.WaP77);
Button image29 = (Button) findViewById(R.id.WaP78);
Button image30 = (Button) findViewById(R.id.WaP79);
Button image31 = (Button) findViewById(R.id.WaP80);
Button image32 = (Button) findViewById(R.id.WaP81);
Button image33 = (Button) findViewById(R.id.WaP82);
Button image34 = (Button) findViewById(R.id.WaP83);
Button image35 = (Button) findViewById(R.id.WaP84);
Button image36 = (Button) findViewById(R.id.WaP85);
Button image37 = (Button) findViewById(R.id.WaP86);
Button image38 = (Button) findViewById(R.id.WaP87);
Button image39 = (Button) findViewById(R.id.WaP88);
Button image40 = (Button) findViewById(R.id.WaP89);
Button image41 = (Button) findViewById(R.id.WaP90);
Button image42 = (Button) findViewById(R.id.WaP91);
Button image43 = (Button) findViewById(R.id.WaP92);
Button image44 = (Button) findViewById(R.id.WaP93);
Button image45 = (Button) findViewById(R.id.WaP94);
Button image46 = (Button) findViewById(R.id.WaP95);
Button image47 = (Button) findViewById(R.id.WaP96);
Button setWall = (Button) findViewById(R.id.SetWallPaperButton);
toPhone = R.drawable.a_aardbeien;
knopje = MediaPlayer.create(getApplicationContext(), R.raw.button);
image1.setOnClickListener(new View.OnClickListener() { // Line 90
@Override
public void onClick(View arg0) {
display.setImageResource(R.drawable.aardmannetje);
toPhone = R.drawable.a_aardmannetje;
knopje.start();
}
});
image2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
display.setImageResource(R.drawable.aardbeien);
toPhone = R.drawable.a_aardbeien;
knopje.start();
}
});
image3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
display.setImageResource(R.drawable.android);
toPhone = R.drawable.a_android;
knopje.start();
}
});
image4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
display.setImageResource(R.drawable.batman);
toPhone = R.drawable.a_batman;
knopje.start();
}
});
image5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
display.setImageResource(R.drawable.bij);
toPhone = R.drawable.a_bij;
knopje.start();
}
});
image6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
display.setImageResource(R.drawable.blaadjes);
toPhone = R.drawable.a_blaadjes;
knopje.start();
}
});
image7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
display.setImageResource(R.drawable.bliksem);
toPhone = R.drawable.a_bliksem;
knopje.start();
}
});
image8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
display.setImageResource(R.drawable.bloemblaadjes);
toPhone = R.drawable.a_bloemblaadjes;
knopje.start();
}
});
image9.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
display.setImageResource(R.drawable.bloemetjes);
toPhone = R.drawable.a_bloemetjes;
knopje.start();
}
});
image10.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
display.setImageResource(R.drawable.blokjes);
toPhone = R.drawable.a_blokjes;
knopje.start();
}
});
image11.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
display.setImageResource(R.drawable.bubbels);
toPhone = R.drawable.a_bubbels;
knopje.start();
}
});
image12.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
display.setImageResource(R.drawable.chocolade);
toPhone = R.drawable.a_chocolade;
knopje.start();
}
});
image13.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
display.setImageResource(R.drawable.closeuproos);
toPhone = R.drawable.a_closeuproos;
knopje.start();
}
});
image14.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
display.setImageResource(R.drawable.druppels);
toPhone = R.drawable.a_druppels;
knopje.start();
}
});
image15.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
display.setImageResource(R.drawable.duinen);
toPhone = R.drawable.a_duinen;
knopje.start();
}
});
image16.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
display.setImageResource(R.drawable.frambozen);
toPhone = R.drawable.a_frambozen;
knopje.start();
}
});
image17.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
display.setImageResource(R.drawable.gekleurdeblokjes);
toPhone = R.drawable.a_gekleurdeblokjes;
knopje.start();
}
});
image18.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
display.setImageResource(R.drawable.gekleurdeballetjes);
toPhone = R.drawable.a_gekleurderondjes;
knopje.start();
}
});
image19.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
display.setImageResource(R.drawable.gekleurderoos);
toPhone = R.drawable.a_gekleurderoos;
knopje.start();
}
});
image20.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
display.setImageResource(R.drawable.geld);
toPhone = R.drawable.a_geld;
knopje.start();
}
});
image21.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
display.setImageResource(R.drawable.gitaar);
toPhone = R.drawable.a_gitaar;
knopje.start();
}
});
image22.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
display.setImageResource(R.drawable.graslucht);
toPhone = R.drawable.a_luchtgras;
knopje.start();
}
});
image23.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
display.setImageResource(R.drawable.hartjes);
toPhone = R.drawable.a_hartjes;
knopje.start();
}
});
image24.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
display.setImageResource(R.drawable.hartjesdruppel);
toPhone = R.drawable.a_hartjesdruppel;
knopje.start();
}
});
image25.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
display.setImageResource(R.drawable.ijsjes);
toPhone = R.drawable.a_ijsjes;
knopje.start();
}
});
image26.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
display.setImageResource(R.drawable.jellybeans);
toPhone = R.drawable.a_jellybeans;
knopje.start();
}
});
image27.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
display.setImageResource(R.drawable.kerstballen);
toPhone = R.drawable.a_kerstballen;
knopje.start();
}
});
image28.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
display.setImageResource(R.drawable.kleurenspiraal);
toPhone = R.drawable.a_kleurenspiraal;
knopje.start();
}
});
image29.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
display.setImageResource(R.drawable.kleurenhout);
toPhone = R.drawable.a_kleurhout;
knopje.start();
}
});
image30.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
display.setImageResource(R.drawable.kleurstrepen);
toPhone = R.drawable.a_kleurstrepen;
knopje.start();
}
});
image31.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
display.setImageResource(R.drawable.kunst);
toPhone = R.drawable.a_kunst;
knopje.start();
}
});
image32.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
display.setImageResource(R.drawable.lente);
toPhone = R.drawable.a_lente;
knopje.start();
}
});
image33.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
display.setImageResource(R.drawable.muziek);
toPhone = R.drawable.a_muziek;
knopje.start();
}
});
image34.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
display.setImageResource(R.drawable.panterprint);
toPhone = R.drawable.a_panterprint;
knopje.start();
}
});
image35.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
display.setImageResource(R.drawable.parijs);
toPhone = R.drawable.a_parijs;
knopje.start();
}
});
image36.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
display.setImageResource(R.drawable.piano);
toPhone = R.drawable.a_piano;
knopje.start();
}
});
image37.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
display.setImageResource(R.drawable.rozen);
toPhone = R.drawable.a_rozen;
knopje.start();
}
});
image38.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
display.setImageResource(R.drawable.rozepaarsebloemetjes);
toPhone = R.drawable.a_rozepaarsebloemetjes;
knopje.start();
}
});
image39.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
display.setImageResource(R.drawable.stenen);
toPhone = R.drawable.a_stenen;
knopje.start();
}
});
image40.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
display.setImageResource(R.drawable.strand);
toPhone = R.drawable.a_strand;
knopje.start();
}
});
image41.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
display.setImageResource(R.drawable.streepjes);
toPhone = R.drawable.a_streepjes;
knopje.start();
}
});
image42.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
display.setImageResource(R.drawable.struik);
toPhone = R.drawable.a_struik;
knopje.start();
}
});
image43.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
display.setImageResource(R.drawable.tjahoezullenwehetnoemen);
toPhone = R.drawable.a_tjahoezullenwehetnoemen;
knopje.start();
}
});
image44.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
display.setImageResource(R.drawable.tulpen);
toPhone = R.drawable.a_tulpen;
knopje.start();
}
});
image45.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
display.setImageResource(R.drawable.veren);
toPhone = R.drawable.a_veren;
knopje.start();
}
});
image46.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
display.setImageResource(R.drawable.watermannetje);
toPhone = R.drawable.a_watermannetje;
knopje.start();
}
});
image47.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
display.setImageResource(R.drawable.zwartwit);
toPhone = R.drawable.a_zwartwit;
knopje.start();
}
});
setWall.setOnClickListener(new View.OnClickListener() {
@SuppressWarnings("deprecation")
@Override
public void onClick(View arg0) {
Bitmap whatever = BitmapFactory.decodeStream(getResources()
.openRawResource(toPhone));
try {
getApplicationContext().setWallpaper(whatever);
} catch (IOException e) {
e.printStackTrace();
} finally {knopje.start();
Toast my_toast = Toast.makeText(getApplicationContext(), "Wallpaper Set!", Toast.LENGTH_SHORT);
my_toast.setGravity(Gravity.CENTER, 0, 0);
my_toast.show();;
}
}
});
};
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
mAdView.destroy();
}
@Override
protected void onDestroy() {
super.onDestroy();
mAdView.destroy();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
mAdView = (MoPubView) findViewById(R.id.adview);
mAdView.setAdUnitId("e95a36beb72611e281c11231392559e4"); // Enter your
// Ad Unit
// ID from
// www.mopub.com
mAdView.loadAd();
}
}
每当我在 480x800 手机上测试我的应用程序时,它都会因 NullPointerExeption 而崩溃。知道那个可能来自哪里吗?
这是日志猫:
05-15 11:27:47.600: E/AndroidRuntime(31642): FATAL EXCEPTION: main
05-15 11:27:47.600: E/AndroidRuntime(31642): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mywallpapers/com.example.mywallpapers.SevenTwentyOne}: java.lang.NullPointerException
05-15 11:27:47.600: E/AndroidRuntime(31642): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
05-15 11:27:47.600: E/AndroidRuntime(31642): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
05-15 11:27:47.600: E/AndroidRuntime(31642): at android.app.ActivityThread.access$600(ActivityThread.java:140)
05-15 11:27:47.600: E/AndroidRuntime(31642): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
05-15 11:27:47.600: E/AndroidRuntime(31642): at android.os.Handler.dispatchMessage(Handler.java:99)
05-15 11:27:47.600: E/AndroidRuntime(31642): at android.os.Looper.loop(Looper.java:137)
05-15 11:27:47.600: E/AndroidRuntime(31642): at android.app.ActivityThread.main(ActivityThread.java:4898)
05-15 11:27:47.600: E/AndroidRuntime(31642): at java.lang.reflect.Method.invokeNative(Native Method)
05-15 11:27:47.600: E/AndroidRuntime(31642): at java.lang.reflect.Method.invoke(Method.java:511)
05-15 11:27:47.600: E/AndroidRuntime(31642): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
05-15 11:27:47.600: E/AndroidRuntime(31642): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
05-15 11:27:47.600: E/AndroidRuntime(31642): at dalvik.system.NativeStart.main(Native Method)
05-15 11:27:47.600: E/AndroidRuntime(31642): Caused by: java.lang.NullPointerException
05-15 11:27:47.600: E/AndroidRuntime(31642): at com.example.mywallpapers.SevenTwentyOne.onCreate(SevenTwentyOne.java:90)
05-15 11:27:47.600: E/AndroidRuntime(31642): at android.app.Activity.performCreate(Activity.java:5206)
05-15 11:27:47.600: E/AndroidRuntime(31642): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
05-15 11:27:47.600: E/AndroidRuntime(31642): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
05-15 11:27:47.600: E/AndroidRuntime(31642): ... 11 more