我一直在网上搜索几个小时,但无济于事。
我一直在开发一款应用程序,根据屏幕分辨率切换到不同的布局,但每当我尝试启动新的Activity时,应用程序崩溃......
这是我的MainActivity,从中调用新类:
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;
import android.widget.Toast;
public class MainActivity extends Activity {
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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();
}
switch (width) {
case 480:
Toast.makeText(getApplicationContext(),
"Your screen width is " + width,
Toast.LENGTH_SHORT).show();
startActivity(new Intent(MainActivity.this,
FourEightyEightHundred.class));
break;
case 720:
Toast.makeText(getApplicationContext(),
"Your screen width is " + width,
Toast.LENGTH_SHORT).show();
break;
}
}
}
这是AndroidManifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mywallpapers"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.mywallpapers.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".FourEightyEightHundred"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.example.mywallpapers.FOUREIGHT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
所谓的活动:
package com.example.mywallpapers;
import java.io.IOException;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class FourEightyEightHundred extends Activity implements View.OnClickListener {
ImageView display;
int toPhone;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.foureightyeighthundred);
display = (ImageView) findViewById(R.id.WaP1);
ImageView image1 = (ImageView) findViewById(R.id.WaP2);
ImageView image2 = (ImageView) findViewById(R.id.WaP3);
ImageView image3 = (ImageView) findViewById(R.id.WaP4);
ImageView image4 = (ImageView) findViewById(R.id.WaP5);
ImageView image5 = (ImageView) findViewById(R.id.WaP6);
ImageView image6 = (ImageView) findViewById(R.id.WaP7);
ImageView image7 = (ImageView) findViewById(R.id.WaP8);
ImageView image8 = (ImageView) findViewById(R.id.WaP9);
ImageView image9 = (ImageView) findViewById(R.id.WaP10);
ImageView image10 = (ImageView) findViewById(R.id.WaP11);
ImageView image11 = (ImageView) findViewById(R.id.WaP12);
ImageView image12 = (ImageView) findViewById(R.id.WaP13);
ImageView image13 = (ImageView) findViewById(R.id.WaP14);
ImageView image14 = (ImageView) findViewById(R.id.WaP15);
ImageView image15 = (ImageView) findViewById(R.id.WaP16);
ImageView image16 = (ImageView) findViewById(R.id.WaP17);
ImageView image17 = (ImageView) findViewById(R.id.WaP18);
ImageView image18 = (ImageView) findViewById(R.id.WaP19);
ImageView image19 = (ImageView) findViewById(R.id.WaP20);
ImageView image20 = (ImageView) findViewById(R.id.WaP21);
ImageView image21 = (ImageView) findViewById(R.id.WaP22);
ImageView image22 = (ImageView) findViewById(R.id.WaP23);
ImageView image23 = (ImageView) findViewById(R.id.WaP24);
ImageView image24 = (ImageView) findViewById(R.id.WaP25);
ImageView image25 = (ImageView) findViewById(R.id.WaP26);
ImageView image26 = (ImageView) findViewById(R.id.WaP27);
ImageView image27 = (ImageView) findViewById(R.id.WaP28);
ImageView image28 = (ImageView) findViewById(R.id.WaP29);
ImageView image29 = (ImageView) findViewById(R.id.WaP30);
ImageView image30 = (ImageView) findViewById(R.id.WaP31);
Button setWallPaper = (Button) findViewById(R.id.SetWallPaperButton);
toPhone = R.drawable.foureightyone;
image1.setOnClickListener(this);
image2.setOnClickListener(this);
image3.setOnClickListener(this);
image4.setOnClickListener(this);
image5.setOnClickListener(this);
image6.setOnClickListener(this);
image7.setOnClickListener(this);
image8.setOnClickListener(this);
image9.setOnClickListener(this);
image10.setOnClickListener(this);
image11.setOnClickListener(this);
image12.setOnClickListener(this);
image13.setOnClickListener(this);
image14.setOnClickListener(this);
image15.setOnClickListener(this);
image16.setOnClickListener(this);
image17.setOnClickListener(this);
image18.setOnClickListener(this);
image19.setOnClickListener(this);
image20.setOnClickListener(this);
image21.setOnClickListener(this);
image22.setOnClickListener(this);
image23.setOnClickListener(this);
image24.setOnClickListener(this);
image25.setOnClickListener(this);
image26.setOnClickListener(this);
image27.setOnClickListener(this);
image28.setOnClickListener(this);
image29.setOnClickListener(this);
image30.setOnClickListener(this);
setWallPaper.setOnClickListener(this);
}
@SuppressWarnings("deprecation")
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()){
case R.id.WaP2:
display.setImageResource(R.drawable.foureightytwo);
toPhone = R.drawable.foureightytwo;
break;
case R.id.WaP3:
display.setImageResource(R.drawable.foureightythree);
toPhone = R.drawable.foureightythree;
break;
case R.id.WaP4:
display.setImageResource(R.drawable.foureightyfour);
toPhone = R.drawable.foureightyfour;
break;
case R.id.WaP5:
display.setImageResource(R.drawable.foureightyfive);
toPhone = R.drawable.foureightyfive;
break;
case R.id.WaP6:
display.setImageResource(R.drawable.foureightysix);
toPhone = R.drawable.foureightysix;
break;
case R.id.WaP7:
display.setImageResource(R.drawable.fortyeightyseven);
toPhone = R.drawable.fortyeightyseven;
break;
case R.id.WaP8:
display.setImageResource(R.drawable.aardbeien);
toPhone = R.drawable.aardbeien;
break;
case R.id.WaP9:
display.setImageResource(R.drawable.android);
toPhone = R.drawable.android;
break;
case R.id.WaP10:
display.setImageResource(R.drawable.bloemetje_gras_lucht);
toPhone = R.drawable.bloemetje_gras_lucht;
break;
case R.id.WaP11:
display.setImageResource(R.drawable.bloemetje);
toPhone = R.drawable.bloemetje;
break;
case R.id.WaP12:
display.setImageResource(R.drawable.druppel_op_blad);
toPhone = R.drawable.druppel_op_blad;
break;
case R.id.WaP13:
display.setImageResource(R.drawable.duin_lucht);
toPhone = R.drawable.duin_lucht;
break;
case R.id.WaP14:
display.setImageResource(R.drawable.frambozen);
toPhone = R.drawable.frambozen;
break;
case R.id.WaP15:
display.setImageResource(R.drawable.duin_lucht);
toPhone = R.drawable.duin_lucht;
break;
case R.id.WaP16:
display.setImageResource(R.drawable.gras_lucht);
toPhone = R.drawable.gras_lucht;
break;
case R.id.WaP17:
display.setImageResource(R.drawable.jelly_beans);
toPhone = R.drawable.jelly_beans;
break;
case R.id.WaP18:
display.setImageResource(R.drawable.katje);
toPhone = R.drawable.katje;
break;
case R.id.WaP19:
display.setImageResource(R.drawable.kerstballen);
toPhone = R.drawable.kerstballen;
break;
case R.id.WaP20:
display.setImageResource(R.drawable.lieveheersbeestje);
toPhone = R.drawable.lieveheersbeestje;
break;
case R.id.WaP21:
display.setImageResource(R.drawable.pinguins);
toPhone = R.drawable.pinguins;
break;
case R.id.WaP22:
display.setImageResource(R.drawable.roze_paarse_bloemetjes);
toPhone = R.drawable.roze_paarse_bloemetjes;
break;
case R.id.WaP23:
display.setImageResource(R.drawable.roze_panterprint);
toPhone = R.drawable.roze_panterprint;
break;
case R.id.WaP24:
display.setImageResource(R.drawable.roze_rozen);
toPhone = R.drawable.roze_rozen;
break;
case R.id.WaP25:
display.setImageResource(R.drawable.stand_zee);
toPhone = R.drawable.stand_zee;
break;
case R.id.WaP26:
display.setImageResource(R.drawable.stenen);
toPhone = R.drawable.stenen;
break;
case R.id.WaP27:
display.setImageResource(R.drawable.stro_rol);
toPhone = R.drawable.stro_rol;
break;
case R.id.WaP28:
display.setImageResource(R.drawable.tijger);
toPhone = R.drawable.tijger;
break;
case R.id.WaP29:
display.setImageResource(R.drawable.tulpen);
toPhone = R.drawable.tulpen;
break;
case R.id.WaP30:
display.setImageResource(R.drawable.watermannetje);
toPhone = R.drawable.watermannetje;
break;
case R.id.WaP31:
display.setImageResource(R.drawable.wolf);
toPhone = R.drawable.wolf;
break;
case R.id.SetWallPaperButton:
Bitmap whatever = BitmapFactory.decodeStream(getResources().openRawResource(toPhone));
try{
getApplicationContext().setWallpaper(whatever);
}
catch( IOException e){
e.printStackTrace();
};
Toast.makeText(this, "Wallpaper set!",Toast.LENGTH_SHORT).show();
break;
}
}
}
和LogCat
05-06 21:52:41.542: E/AndroidRuntime(1097): FATAL EXCEPTION: main
05-06 21:52:41.542: E/AndroidRuntime(1097): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mywallpapers/com.example.mywallpapers.FourEightyEightHundred}: android.view.InflateException: Binary XML file line #103: Error inflating class <unknown>
05-06 21:52:41.542: E/AndroidRuntime(1097): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
05-06 21:52:41.542: E/AndroidRuntime(1097): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
05-06 21:52:41.542: E/AndroidRuntime(1097): at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-06 21:52:41.542: E/AndroidRuntime(1097): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
05-06 21:52:41.542: E/AndroidRuntime(1097): at android.os.Handler.dispatchMessage(Handler.java:99)
05-06 21:52:41.542: E/AndroidRuntime(1097): at android.os.Looper.loop(Looper.java:137)
05-06 21:52:41.542: E/AndroidRuntime(1097): at android.app.ActivityThread.main(ActivityThread.java:5041)
05-06 21:52:41.542: E/AndroidRuntime(1097): at java.lang.reflect.Method.invokeNative(Native Method)
05-06 21:52:41.542: E/AndroidRuntime(1097): at java.lang.reflect.Method.invoke(Method.java:511)
05-06 21:52:41.542: E/AndroidRuntime(1097): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-06 21:52:41.542: E/AndroidRuntime(1097): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-06 21:52:41.542: E/AndroidRuntime(1097): at dalvik.system.NativeStart.main(Native Method)
05-06 21:52:41.542: E/AndroidRuntime(1097): Caused by: android.view.InflateException: Binary XML file line #103: Error inflating class <unknown>
05-06 21:52:41.542: E/AndroidRuntime(1097): at android.view.LayoutInflater.createView(LayoutInflater.java:613)
05-06 21:52:41.542: E/AndroidRuntime(1097): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
05-06 21:52:41.542: E/AndroidRuntime(1097): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)
05-06 21:52:41.542: E/AndroidRuntime(1097): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
05-06 21:52:41.542: E/AndroidRuntime(1097): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
05-06 21:52:41.542: E/AndroidRuntime(1097): at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
05-06 21:52:41.542: E/AndroidRuntime(1097): at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
05-06 21:52:41.542: E/AndroidRuntime(1097): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
05-06 21:52:41.542: E/AndroidRuntime(1097): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
05-06 21:52:41.542: E/AndroidRuntime(1097): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
05-06 21:52:41.542: E/AndroidRuntime(1097): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
05-06 21:52:41.542: E/AndroidRuntime(1097): at android.app.Activity.setContentView(Activity.java:1881)
05-06 21:52:41.542: E/AndroidRuntime(1097): at com.example.mywallpapers.FourEightyEightHundred.onCreate(FourEightyEightHundred.java:28)
05-06 21:52:41.542: E/AndroidRuntime(1097): at android.app.Activity.performCreate(Activity.java:5104)
05-06 21:52:41.542: E/AndroidRuntime(1097): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
05-06 21:52:41.542: E/AndroidRuntime(1097): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
05-06 21:52:41.542: E/AndroidRuntime(1097): ... 11 more
05-06 21:52:41.542: E/AndroidRuntime(1097): Caused by: java.lang.reflect.InvocationTargetException
05-06 21:52:41.542: E/AndroidRuntime(1097): at java.lang.reflect.Constructor.constructNative(Native Method)
05-06 21:52:41.542: E/AndroidRuntime(1097): at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
05-06 21:52:41.542: E/AndroidRuntime(1097): at android.view.LayoutInflater.createView(LayoutInflater.java:587)
05-06 21:52:41.542: E/AndroidRuntime(1097): ... 26 more
05-06 21:52:41.542: E/AndroidRuntime(1097): Caused by: java.lang.OutOfMemoryError
05-06 21:52:41.542: E/AndroidRuntime(1097): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
05-06 21:52:41.542: E/AndroidRuntime(1097): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:502)
05-06 21:52:41.542: E/AndroidRuntime(1097): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:355)
05-06 21:52:41.542: E/AndroidRuntime(1097): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:785)
05-06 21:52:41.542: E/AndroidRuntime(1097): at android.content.res.Resources.loadDrawable(Resources.java:1965)
05-06 21:52:41.542: E/AndroidRuntime(1097): at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
05-06 21:52:41.542: E/AndroidRuntime(1097): at android.widget.ImageView.<init>(ImageView.java:120)
05-06 21:52:41.542: E/AndroidRuntime(1097): at android.widget.ImageView.<init> (ImageView.java:110)
05-06 21:52:41.542: E/AndroidRuntime(1097): ... 29 more
我想要开始的活动的布局xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/kiezen" />
<ImageView
android:id="@+id/WaP1"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_below="@id/textView1"
android:layout_centerHorizontal="true"
android:contentDescription="@string/Display"
android:src="@drawable/foureightyone" />
<Button
android:id="@+id/SetWallPaperButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/WaP1"
android:gravity="center"
android:text="@string/ZetKnop" />
<ScrollView
android:id="@+id/ScrollWallPapers"
android:layout_width="fill_parent"
android:layout_height="140dp"
android:layout_below="@id/SetWallPaperButton"
android:gravity="center" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="156dp"
android:orientation="horizontal" >
<ImageView
android:id="@+id/WaP2"
android:layout_width="120dp"
android:layout_height="fill_parent"
android:contentDescription="@string/Display"
android:src="@drawable/foureightytwo" />
<ImageView
android:id="@+id/WaP3"
android:layout_width="120dp"
android:layout_height="fill_parent"
android:contentDescription="@string/Display"
android:src="@drawable/foureightythree" />
<ImageView
android:id="@+id/WaP4"
android:layout_width="120dp"
android:layout_height="fill_parent"
android:contentDescription="@string/Display"
android:src="@drawable/foureightyfour" />
<ImageView
android:id="@+id/WaP5"
android:layout_width="120dp"
android:layout_height="fill_parent"
android:contentDescription="@string/Display"
android:src="@drawable/foureightyfive" />
<ImageView
android:id="@+id/WaP6"
android:layout_width="120dp"
android:layout_height="fill_parent"
android:contentDescription="@string/Display"
android:src="@drawable/foureightysix" />
<ImageView
android:id="@+id/WaP7"
android:layout_width="120dp"
android:layout_height="fill_parent"
android:contentDescription="@string/Display"
android:src="@drawable/fortyeightyseven" />
<ImageView
android:id="@+id/WaP8"
android:layout_width="120dp"
android:layout_height="fill_parent"
android:contentDescription="@string/Display"
android:src="@drawable/aardbeien" />
<ImageView
android:id="@+id/WaP9"
android:layout_width="120dp"
android:layout_height="fill_parent"
android:contentDescription="@string/Display"
android:src="@drawable/android" />
<ImageView
android:id="@+id/WaP10"
android:layout_width="120dp"
android:layout_height="fill_parent"
android:contentDescription="@string/Display"
android:src="@drawable/bloemetje_gras_lucht" />
<ImageView
android:id="@+id/WaP11"
android:layout_width="120dp"
android:layout_height="fill_parent"
android:contentDescription="@string/Display"
android:src="@drawable/bloemetje" />
<ImageView
android:id="@+id/WaP12"
android:layout_width="120dp"
android:layout_height="fill_parent"
android:contentDescription="@string/Display"
android:src="@drawable/druppel_op_blad" />
<ImageView
android:id="@+id/WaP13"
android:layout_width="120dp"
android:layout_height="fill_parent"
android:contentDescription="@string/Display"
android:src="@drawable/duin_lucht" />
<ImageView
android:id="@+id/WaP14"
android:layout_width="120dp"
android:layout_height="fill_parent"
android:contentDescription="@string/Display"
android:src="@drawable/frambozen" />
<ImageView
android:id="@+id/WaP15"
android:layout_width="120dp"
android:layout_height="fill_parent"
android:contentDescription="@string/Display"
android:src="@drawable/gekleurde_bellen" />
<ImageView
android:id="@+id/WaP16"
android:layout_width="120dp"
android:layout_height="fill_parent"
android:contentDescription="@string/Display"
android:src="@drawable/gras_lucht" />
<ImageView
android:id="@+id/WaP17"
android:layout_width="120dp"
android:layout_height="fill_parent"
android:contentDescription="@string/Display"
android:src="@drawable/jelly_beans" />
<ImageView
android:id="@+id/WaP18"
android:layout_width="120dp"
android:layout_height="fill_parent"
android:contentDescription="@string/Display"
android:src="@drawable/katje" />
<ImageView
android:id="@+id/WaP19"
android:layout_width="120dp"
android:layout_height="fill_parent"
android:contentDescription="@string/Display"
android:src="@drawable/kerstballen" />
<ImageView
android:id="@+id/WaP20"
android:layout_width="120dp"
android:layout_height="fill_parent"
android:contentDescription="@string/Display"
android:src="@drawable/lieveheersbeestje" />
<ImageView
android:id="@+id/WaP21"
android:layout_width="120dp"
android:layout_height="fill_parent"
android:contentDescription="@string/Display"
android:src="@drawable/pinguins" />
<ImageView
android:id="@+id/WaP22"
android:layout_width="120dp"
android:layout_height="fill_parent"
android:contentDescription="@string/Display"
android:src="@drawable/roze_paarse_bloemetjes" />
<ImageView
android:id="@+id/WaP23"
android:layout_width="120dp"
android:layout_height="fill_parent"
android:contentDescription="@string/Display"
android:src="@drawable/roze_panterprint" />
<ImageView
android:id="@+id/WaP24"
android:layout_width="120dp"
android:layout_height="fill_parent"
android:contentDescription="@string/Display"
android:src="@drawable/roze_rozen" />
<ImageView
android:id="@+id/WaP25"
android:layout_width="120dp"
android:layout_height="fill_parent"
android:contentDescription="@string/Display"
android:src="@drawable/stand_zee" />
<ImageView
android:id="@+id/WaP26"
android:layout_width="120dp"
android:layout_height="fill_parent"
android:contentDescription="@string/Display"
android:src="@drawable/stenen" />
<ImageView
android:id="@+id/WaP27"
android:layout_width="120dp"
android:layout_height="fill_parent"
android:contentDescription="@string/Display"
android:src="@drawable/stro_rol" />
<ImageView
android:id="@+id/WaP28"
android:layout_width="120dp"
android:layout_height="fill_parent"
android:contentDescription="@string/Display"
android:src="@drawable/tijger" />
<ImageView
android:id="@+id/WaP29"
android:layout_width="120dp"
android:layout_height="fill_parent"
android:contentDescription="@string/Display"
android:src="@drawable/tulpen" />
<ImageView
android:id="@+id/WaP30"
android:layout_width="120dp"
android:layout_height="fill_parent"
android:contentDescription="@string/Display"
android:src="@drawable/watermannetje" />
<ImageView
android:id="@+id/WaP31"
android:layout_width="120dp"
android:layout_height="fill_parent"
android:contentDescription="@string/Display"
android:src="@drawable/wolf" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
有什么建议吗?
答案 0 :(得分:0)
我需要删除finish()方法并减少页面内所需的CPU数量。我用按钮替换了图像,这有助于;)