我正在尝试从操作栏项目itemSetting打开活动Setting.java。 使用Intent。我已经注册了这两个活动..点击itemSetting it throw error .. activity_main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/d10"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:scaleType="centerInside"
android:src="@drawable/ten_closehihat" />
<ImageView
android:id="@+id/d3"
android:layout_width="250dp"
android:layout_height="250dp"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/d8"
android:layout_marginTop="16dp"
android:scaleType="centerInside"
android:src="@drawable/three_openhihat" />
<ImageView
android:id="@+id/d11"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_alignLeft="@+id/d10"
android:layout_alignParentTop="true"
android:scaleType="centerInside"
android:src="@drawable/elevan_china" />
<ImageView
android:id="@+id/d4"
android:layout_width="250dp"
android:layout_height="250dp"
android:layout_alignTop="@+id/d5"
android:layout_marginLeft="20dp"
android:layout_toRightOf="@+id/d6"
android:scaleType="centerInside"
android:src="@drawable/four_midtom" />
<ImageView
android:id="@+id/d7"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_alignParentBottom="true"
android:layout_alignRight="@+id/d8"
android:scaleType="centerInside"
android:src="@drawable/seven_floortom" />
<ImageView
android:id="@+id/d2"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_alignBottom="@+id/d5"
android:layout_toLeftOf="@+id/d10"
android:scaleType="centerInside"
android:src="@drawable/two_ride" />
<ImageView
android:id="@+id/d8"
android:layout_width="190dp"
android:layout_height="190dp"
android:layout_alignLeft="@+id/d2"
android:layout_alignParentTop="true"
android:layout_marginLeft="90dp"
android:scaleType="centerInside"
android:src="@drawable/eight_crash" />
<ImageView
android:id="@+id/d5"
android:layout_width="250dp"
android:layout_height="250dp"
android:layout_alignTop="@+id/d3"
android:layout_marginRight="39dp"
android:layout_toLeftOf="@+id/d7"
android:scaleType="centerInside"
android:src="@drawable/five_hitom" />
<ImageView
android:id="@+id/d6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:scaleType="centerInside"
android:src="@drawable/six_bassdrum" />
<ImageView
android:id="@+id/d1"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_below="@+id/d9"
android:layout_toLeftOf="@+id/d7"
android:scaleType="centerInside"
android:src="@drawable/one_snare" />
<ImageView
android:id="@+id/d9"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:scaleType="centerInside"
android:src="@drawable/nine_crash" />
</RelativeLayout>
MainActivity.java
package com.example.mydrum;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
public class MainActivity extends Activity {
ImageView d1, d2, d3, d4, d5, d6, d7, d8, d9, d10, d11;
MediaPlayer mp;
final Context context = this;
Intent i;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
d1 = (ImageView) findViewById(R.id.d1);
d2 = (ImageView) findViewById(R.id.d2);
d3 = (ImageView) findViewById(R.id.d3);
d4 = (ImageView) findViewById(R.id.d4);
d5 = (ImageView) findViewById(R.id.d5);
d6 = (ImageView) findViewById(R.id.d6);
d7 = (ImageView) findViewById(R.id.d7);
d8 = (ImageView) findViewById(R.id.d8);
d9 = (ImageView) findViewById(R.id.d9);
d10 = (ImageView) findViewById(R.id.d10);
d11 = (ImageView) findViewById(R.id.d11);
d1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mp = MediaPlayer.create(getApplicationContext(), R.raw.snare);
if (mp != null) {
mp.start();
}
mp.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.release();
}
});
}
});
d2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mp = MediaPlayer.create(getApplicationContext(), R.raw.ride);
if (mp != null) {
mp.start();
}
mp.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.release();
}
});
}
});
d3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mp = MediaPlayer.create(getApplicationContext(),
R.raw.openhihat);
if (mp != null) {
mp.start();
}
mp.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.release();
}
});
}
});
d4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mp = MediaPlayer.create(getApplicationContext(), R.raw.midtom);
if (mp != null) {
mp.start();
}
mp.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.release();
}
});
}
});
d5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mp = MediaPlayer.create(getApplicationContext(), R.raw.hitom);
if (mp != null) {
mp.start();
}
mp.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.release();
}
});
}
});
d6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mp = MediaPlayer
.create(getApplicationContext(), R.raw.bassdrum);
if (mp != null) {
mp.start();
}
mp.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.release();
}
});
}
});
d7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mp = MediaPlayer
.create(getApplicationContext(), R.raw.floortom);
if (mp != null) {
mp.start();
}
mp.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.release();
}
});
}
});
d8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mp = MediaPlayer.create(getApplicationContext(), R.raw.crash2);
if (mp != null) {
mp.start();
}
mp.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.release();
}
});
}
});
d9.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mp = MediaPlayer.create(getApplicationContext(), R.raw.crash);
if (mp != null) {
mp.start();
}
mp.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.release();
}
});
}
});
d10.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mp = MediaPlayer.create(getApplicationContext(),
R.raw.closehihat);
if (mp != null) {
mp.start();
}
mp.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.release();
}
});
}
});
d11.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mp = MediaPlayer.create(getApplicationContext(), R.raw.china);
if (mp != null) {
mp.start();
}
mp.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.release();
}
});
}
});
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater m = getMenuInflater();
m.inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.itemSetting:
i = new Intent(getApplicationContext(), Setting.class);
startActivity(i);
return true;
}
return super.onOptionsItemSelected(item);
}
}
`
custom_setting.xml
<ImageButton
android:id="@+id/btnReset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="160dp"
android:src="@drawable/reset" />
<ImageButton
android:id="@+id/btnMove"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/btnReset"
android:layout_alignLeft="@id/btnReset"
android:src="@drawable/move" />
</RelativeLayout>
Setting.java
package com.example.mydrum;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageButton;
public class Setting extends Activity {
ImageButton btnMove, btnReset;
// android:theme="?android:style/Theme.Holo.Light.Dialog"
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_setting);
btnMove = (ImageButton) findViewById(R.id.btnMove);
btnReset = (ImageButton) findViewById(R.id.btnReset);
}
}
menifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mydrum"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".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="com.example.mydrum.Setting"
>
</activity>
</application>
</manifest>
menu -> main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.mydrum.MainActivity" >
<item
android:id="@+id/itemSetting"
android:showAsAction="always"
android:title="Setting"
android:icon="@drawable/setting"/>
</menu>
plz提供解决方案.. 提前谢谢你
log cat
02-03 13:31:20.891: W/dalvikvm(1937): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
02-03 13:31:20.972: E/AndroidRuntime(1937): FATAL EXCEPTION: main
02-03 13:31:20.972: E/AndroidRuntime(1937): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mydrum/com.example.mydrum.Setting}: android.view.InflateException: Binary XML file line #1: Error inflating class <unknown>
02-03 13:31:20.972: E/AndroidRuntime(1937): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
02-03 13:31:20.972: E/AndroidRuntime(1937): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
02-03 13:31:20.972: E/AndroidRuntime(1937): at android.app.ActivityThread.access$600(ActivityThread.java:122)
02-03 13:31:20.972: E/AndroidRuntime(1937): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
02-03 13:31:20.972: E/AndroidRuntime(1937): at android.os.Handler.dispatchMessage(Handler.java:99)
02-03 13:31:20.972: E/AndroidRuntime(1937): at android.os.Looper.loop(Looper.java:137)
02-03 13:31:20.972: E/AndroidRuntime(1937): at android.app.ActivityThread.main(ActivityThread.java:4340)
02-03 13:31:20.972: E/AndroidRuntime(1937): at java.lang.reflect.Method.invokeNative(Native Method)
02-03 13:31:20.972: E/AndroidRuntime(1937): at java.lang.reflect.Method.invoke(Method.java:511)
02-03 13:31:20.972: E/AndroidRuntime(1937): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-03 13:31:20.972: E/AndroidRuntime(1937): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-03 13:31:20.972: E/AndroidRuntime(1937): at dalvik.system.NativeStart.main(Native Method)
02-03 13:31:20.972: E/AndroidRuntime(1937): Caused by: android.view.InflateException: Binary XML file line #1: Error inflating class <unknown>
02-03 13:31:20.972: E/AndroidRuntime(1937): at android.view.LayoutInflater.createView(LayoutInflater.java:606)
02-03 13:31:20.972: E/AndroidRuntime(1937): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
02-03 13:31:20.972: E/AndroidRuntime(1937): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:653)
02-03 13:31:20.972: E/AndroidRuntime(1937): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:678)
02-03 13:31:20.972: E/AndroidRuntime(1937): at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
02-03 13:31:20.972: E/AndroidRuntime(1937): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
02-03 13:31:20.972: E/AndroidRuntime(1937): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
02-03 13:31:20.972: E/AndroidRuntime(1937): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:251)
02-03 13:31:20.972: E/AndroidRuntime(1937): at android.app.Activity.setContentView(Activity.java:1835)
02-03 13:31:20.972: E/AndroidRuntime(1937): at com.example.mydrum.Setting.onCreate(Setting.java:15)
02-03 13:31:20.972: E/AndroidRuntime(1937): at android.app.Activity.performCreate(Activity.java:4465)
02-03 13:31:20.972: E/AndroidRuntime(1937): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
02-03 13:31:20.972: E/AndroidRuntime(1937): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
02-03 13:31:20.972: E/AndroidRuntime(1937): ... 11 more
02-03 13:31:20.972: E/AndroidRuntime(1937): Caused by: java.lang.reflect.InvocationTargetException
02-03 13:31:20.972: E/AndroidRuntime(1937): at java.lang.reflect.Constructor.constructNative(Native Method)
02-03 13:31:20.972: E/AndroidRuntime(1937): at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
02-03 13:31:20.972: E/AndroidRuntime(1937): at android.view.LayoutInflater.createView(LayoutInflater.java:586)
02-03 13:31:20.972: E/AndroidRuntime(1937): ... 23 more
02-03 13:31:20.972: E/AndroidRuntime(1937): Caused by: java.lang.OutOfMemoryError
02-03 13:31:20.972: E/AndroidRuntime(1937): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
02-03 13:31:20.972: E/AndroidRuntime(1937): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:483)
02-03 13:31:20.972: E/AndroidRuntime(1937): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:351)
02-03 13:31:20.972: E/AndroidRuntime(1937): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:773)
02-03 13:31:20.972: E/AndroidRuntime(1937): at android.content.res.Resources.loadDrawable(Resources.java:1937)
02-03 13:31:20.972: E/AndroidRuntime(1937): at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
02-03 13:31:20.972: E/AndroidRuntime(1937): at android.view.View.<init>(View.java:2780)
02-03 13:31:20.972: E/AndroidRuntime(1937): at android.view.View.<init>(View.java:2717)
02-03 13:31:20.972: E/AndroidRuntime(1937): at android.view.ViewGroup.<init>(ViewGroup.java:379)
02-03 13:31:20.972: E/AndroidRuntime(1937): at android.widget.RelativeLayout.<init>(RelativeLayout.java:174)
02-03 13:31:20.972: E/AndroidRuntime(1937): ... 26 more
答案 0 :(得分:0)
如果MainActivity和Setting都在同一个包中
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mydrum"
android:installLocation="auto"
android:versionCode="1"
android:versionName="1">
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".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=".Setting"
>
</activity>
</application>
</manifest>