请帮助我,我是Android编程的新手。 :( 这是不断崩溃的应用程序。 如果我自己运行这个应用程序,它的工作完美! 的 TutorialThree.java
package com.example.tutorialbasics;
import java.io.IOException;
import android.app.Activity;
import android.app.WallpaperManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class TutorialThree extends Activity implements View.OnClickListener {
ImageView display;
int toPhone;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.tutorial3);
toPhone= R.drawable.back_android1;
display = (ImageView) findViewById(R.id.IVDisplay);
ImageView img1 = (ImageView) findViewById(R.id.IVImage1);
ImageView img2 = (ImageView) findViewById(R.id.IVImage2);
ImageView img3 = (ImageView) findViewById(R.id.IVImage3);
ImageView img4 = (ImageView) findViewById(R.id.IVImage4);
ImageView img5 = (ImageView) findViewById(R.id.IVImage5);
ImageView img6 = (ImageView) findViewById(R.id.IVImage6);
Button setWall = (Button) findViewById(R.id.bSetWallpaper);
img1.setOnClickListener(this);
img2.setOnClickListener(this);
img3.setOnClickListener(this);
img4.setOnClickListener(this);
img5.setOnClickListener(this);
img6.setOnClickListener(this);
setWall.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()){
case R.id.IVImage1:
display.setImageResource(R.drawable.back_android1);
toPhone= R.drawable.back_android1;
break;
case R.id.IVImage2:
display.setImageResource(R.drawable.back_android2);
toPhone= R.drawable.back_android2;
break;
case R.id.IVImage3:
display.setImageResource(R.drawable.back_android3);
toPhone= R.drawable.back_android3;
break;
case R.id.IVImage4:
display.setImageResource(R.drawable.back_android4);
toPhone= R.drawable.back_android4;
break;
case R.id.IVImage5:
display.setImageResource(R.drawable.back_android5);
toPhone= R.drawable.back_android5;
break;
case R.id.IVImage6:
display.setImageResource(R.drawable.back_android6);
toPhone= R.drawable.back_android6;
break;
case R.id.bSetWallpaper:
try{
WallpaperManager.getInstance(getApplicationContext()).setResource(toPhone);
}catch(IOException e){
e.printStackTrace();
}
break;
}
}
}
Tutorial3.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/IVDisplay"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center"
android:src="@drawable/back_android1" >
</ImageView>
<Button
android:id="@+id/bSetWallpaper"
android:layout_gravity="center"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="Set Background" >
</Button>
<HorizontalScrollView
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="@+id/IVImage1"
android:layout_width="125dp"
android:layout_height="125dp"
android:padding="15dp"
android:src="@drawable/back_android1" >
</ImageView>
<ImageView
android:id="@+id/IVImage2"
android:layout_width="125dp"
android:layout_height="125dp"
android:padding="15dp"
android:src="@drawable/back_android2" >
</ImageView>
<ImageView
android:id="@+id/IVImage3"
android:layout_width="125dp"
android:layout_height="125dp"
android:padding="15dp"
android:src="@drawable/back_android3" >
</ImageView>
<ImageView
android:id="@+id/IVImage4"
android:layout_width="125dp"
android:layout_height="125dp"
android:padding="15dp"
android:src="@drawable/back_android4" >
</ImageView>
<ImageView
android:id="@+id/IVImage5"
android:layout_width="125dp"
android:layout_height="125dp"
android:padding="15dp"
android:src="@drawable/back_android5" >
</ImageView>
<ImageView
android:id="@+id/IVImage6"
android:layout_width="125dp"
android:layout_height="125dp"
android:padding="15dp"
android:src="@drawable/back_android6" >
</ImageView>
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
一旦我通过主应用程序中的startActivity调用它就会崩溃。 主要应用 - MyMenu.java
package com.example.tutorialbasics;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MyMenu extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// set up the button
final MediaPlayer mpButtonClick1 = MediaPlayer.create(this,
R.raw.button_click1);
final Intent i = new Intent(this, TutorialOne.class);
final Intent i2 = new Intent(this, listV.class);
final Intent i3 = new Intent(this, TutorialThree.class);
// button1
Button bTutorial1 = (Button) findViewById(R.id.bTutorial1);
bTutorial1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(i);
mpButtonClick1.start();
}
});
// button2
Button bTutorial2 = (Button) findViewById(R.id.bTutorial2);
bTutorial2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(i2);
mpButtonClick1.start();
}
});
// button3
Button bTutorial3 = (Button) findViewById(R.id.bTutorial3);
bTutorial3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(i3);
mpButtonClick1.start();
}
});
// button4
Button bTutorial4 = (Button) findViewById(R.id.bTutorial4);
bTutorial4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mpButtonClick1.start();
}
});
// button5
Button bTutorial5 = (Button) findViewById(R.id.bTutorial5);
bTutorial5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mpButtonClick1.start();
}
});
// button6
Button bTutorial6 = (Button) findViewById(R.id.bTutorial6);
bTutorial6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mpButtonClick1.start();
}
});
// button7
Button bTutorial7 = (Button) findViewById(R.id.bTutorial7);
bTutorial7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mpButtonClick1.start();
}
});
// button8
Button bTutorial8 = (Button) findViewById(R.id.bTutorial8);
bTutorial8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mpButtonClick1.start();
}
});
// button9
Button bTutorial9 = (Button) findViewById(R.id.bTutorial9);
bTutorial9.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mpButtonClick1.start();
}
});
}
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater awesome = getMenuInflater();
awesome.inflate(R.menu.main_menu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menuSweet:
Intent i = new Intent(this, sweet.class);
startActivity(i);
return true;
case R.id.menuToast:
Toast display = Toast.makeText(this, "Welcome", Toast.LENGTH_SHORT);
display.show();
return true;
}
return false;
}
}
main.xml中
<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@drawable/orange" >
<ScrollView
android:layout_width="fill_parent"
android:layout_height="200sp" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:layout_width="fill_parent"
android:layout_height="60dp"
android:text="Radio Buttons"
android:textSize="20sp"
android:id="@+id/bTutorial1" >
</Button>
<Button
android:layout_width="fill_parent"
android:layout_height="60dp"
android:text="List View"
android:textSize="20sp"
android:id="@+id/bTutorial2" >
</Button>
<Button
android:layout_width="fill_parent"
android:layout_height="60dp"
android:text="Wallpaper"
android:textSize="20sp"
android:id="@+id/bTutorial3" >
</Button>
<Button
android:layout_width="fill_parent"
android:layout_height="60dp"
android:text="Tutorial4"
android:textSize="20sp"
android:id="@+id/bTutorial4" >
</Button>
<Button
android:layout_width="fill_parent"
android:layout_height="60dp"
android:text="Tutorial5"
android:textSize="20sp"
android:id="@+id/bTutorial5" >
</Button>
<Button
android:layout_width="fill_parent"
android:layout_height="60dp"
android:text="Tutorial6"
android:textSize="20sp"
android:id="@+id/bTutorial6" >
</Button>
<Button
android:layout_width="fill_parent"
android:layout_height="60dp"
android:text="Tutorial7"
android:textSize="20sp"
android:id="@+id/bTutorial7" >
</Button>
<Button
android:layout_width="fill_parent"
android:layout_height="60dp"
android:text="Tutorial8"
android:textSize="20sp"
android:id="@+id/bTutorial8" >
</Button>
<Button
android:layout_width="fill_parent"
android:layout_height="60dp"
android:text="Tutorial9"
android:textSize="20sp"
android:id="@+id/bTutorial9" >
</Button>
</LinearLayout>
</ScrollView>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:fontFamily="Copper"
android:text="\nDada Acuin"
android:textStyle="bold"
android:textSize="40sp"
></TextView>
</LinearLayout>
这是我的清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.tutorialbasics"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.SET_WALLPAPER" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MyMenu"
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=".MyMain"
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=".sweet"
android:label="@string/app_name"
android:theme="@android:style/Theme.Dialog">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".TutorialOne"
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=".listV"
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=".TutorialThree"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
这是logcat的错误 Aneeq Anwar。我试图使用你给的代码......但它仍然崩溃..
这是我的logcat
06-17 03:24:06.406: E/dalvikvm-heap(1051): Out of memory on a 9216016-byte allocation.
06-17 03:24:06.906: E/AndroidRuntime(1051): FATAL EXCEPTION: main
06-17 03:24:06.906: E/AndroidRuntime(1051): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tutorialbasics/com.example.tutorialbasics.TutorialThree}: android.view.InflateException: Binary XML file line #41: Error inflating class <unknown>
06-17 03:24:06.906: E/AndroidRuntime(1051): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
06-17 03:24:06.906: E/AndroidRuntime(1051): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
06-17 03:24:06.906: E/AndroidRuntime(1051): at android.app.ActivityThread.access$600(ActivityThread.java:141)
06-17 03:24:06.906: E/AndroidRuntime(1051): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
06-17 03:24:06.906: E/AndroidRuntime(1051): at android.os.Handler.dispatchMessage(Handler.java:99)
06-17 03:24:06.906: E/AndroidRuntime(1051): at android.os.Looper.loop(Looper.java:137)
06-17 03:24:06.906: E/AndroidRuntime(1051): at android.app.ActivityThread.main(ActivityThread.java:5041)
06-17 03:24:06.906: E/AndroidRuntime(1051): at java.lang.reflect.Method.invokeNative(Native Method)
06-17 03:24:06.906: E/AndroidRuntime(1051): at java.lang.reflect.Method.invoke(Method.java:511)
06-17 03:24:06.906: E/AndroidRuntime(1051): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-17 03:24:06.906: E/AndroidRuntime(1051): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-17 03:24:06.906: E/AndroidRuntime(1051): at dalvik.system.NativeStart.main(Native Method)
06-17 03:24:06.906: E/AndroidRuntime(1051): Caused by: android.view.InflateException: Binary XML file line #41: Error inflating class <unknown>
06-17 03:24:06.906: E/AndroidRuntime(1051): at android.view.LayoutInflater.createView(LayoutInflater.java:613)
06-17 03:24:06.906: E/AndroidRuntime(1051): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
06-17 03:24:06.906: E/AndroidRuntime(1051): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)
06-17 03:24:06.906: E/AndroidRuntime(1051): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
06-17 03:24:06.906: E/AndroidRuntime(1051): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
06-17 03:24:06.906: E/AndroidRuntime(1051): at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
06-17 03:24:06.906: E/AndroidRuntime(1051): at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
06-17 03:24:06.906: E/AndroidRuntime(1051): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
06-17 03:24:06.906: E/AndroidRuntime(1051): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
06-17 03:24:06.906: E/AndroidRuntime(1051): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
06-17 03:24:06.906: E/AndroidRuntime(1051): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
06-17 03:24:06.906: E/AndroidRuntime(1051): at android.app.Activity.setContentView(Activity.java:1881)
06-17 03:24:06.906: E/AndroidRuntime(1051): at com.example.tutorialbasics.TutorialThree.onCreate(TutorialThree.java:24)
06-17 03:24:06.906: E/AndroidRuntime(1051): at android.app.Activity.performCreate(Activity.java:5104)
06-17 03:24:06.906: E/AndroidRuntime(1051): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
06-17 03:24:06.906: E/AndroidRuntime(1051): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
06-17 03:24:06.906: E/AndroidRuntime(1051): ... 11 more
06-17 03:24:06.906: E/AndroidRuntime(1051): Caused by: java.lang.reflect.InvocationTargetException
06-17 03:24:06.906: E/AndroidRuntime(1051): at java.lang.reflect.Constructor.constructNative(Native Method)
06-17 03:24:06.906: E/AndroidRuntime(1051): at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
06-17 03:24:06.906: E/AndroidRuntime(1051): at android.view.LayoutInflater.createView(LayoutInflater.java:587)
06-17 03:24:06.906: E/AndroidRuntime(1051): ... 26 more
06-17 03:24:06.906: E/AndroidRuntime(1051): Caused by: java.lang.OutOfMemoryError
06-17 03:24:06.906: E/AndroidRuntime(1051): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
06-17 03:24:06.906: E/AndroidRuntime(1051): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:502)
06-17 03:24:06.906: E/AndroidRuntime(1051): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:785)
06-17 03:24:06.906: E/AndroidRuntime(1051): at android.content.res.Resources.loadDrawable(Resources.java:1965)
06-17 03:24:06.906: E/AndroidRuntime(1051): at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
06-17 03:24:06.906: E/AndroidRuntime(1051): at android.widget.ImageView.<init>(ImageView.java:120)
06-17 03:24:06.906: E/AndroidRuntime(1051): at android.widget.ImageView.<init>(ImageView.java:110)
06-17 03:24:06.906: E/AndroidRuntime(1051): ... 29 more
答案 0 :(得分:1)
AndroidManifest.xml中存在错误,请将其更改为
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.SET_WALLPAPER" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MyMenu"
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=".MyMain"
android:label="@string/app_name" >
</activity>
<activity
android:name=".sweet"
android:label="@string/app_name"
android:theme="@android:style/Theme.Dialog">
</activity>
<activity
android:name=".TutorialOne"
android:label="@string/app_name" >
</activity>
<activity
android:name=".listV"
android:label="@string/app_name" >
</activity>
<activity
android:name=".TutorialThree"
android:label="@string/app_name" >
</activity>
</application>
答案 1 :(得分:0)
删除除Launcher活动之外的所有意图过滤。
即,应该只有一次LAUNCHER活动。