我的应用程序有两个按钮,上面有“更多信息”的按钮指向另一个活动。但如果我点击按钮它会崩溃。我已经在清单文件中包含了活动和意图。这是我的主要活动
public class MainActivity extends Activity {
Integer[] pics = {
R.drawable.rah,
R.drawable.rah18a,
};
ImageView imageView;
Button Button1;
Button Button2;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Gallery ga = (Gallery)findViewById(R.id.Gallery01);
ga.setAdapter(new ImageAdapter(this));
imageView = (ImageView)findViewById(R.id.ImageView01);
Button1 = (Button)findViewById(R.id.button1);
Button2 = (Button) findViewById(R.id.button2);
Button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent("Description.Main.Activity2");
startActivity(intent);
}
});
ga.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, final int pos,
long arg3) {
imageView.setImageResource(pics[pos]);
Button1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
WallpaperManager wm = WallpaperManager.getInstance(v.getContext());
try {
wm.setResource(pics[pos]);
Toast.makeText(getBaseContext(),
"Wallpaper has been set",
Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
});
}
public class ImageAdapter extends BaseAdapter {
private Context ctx;
int imageBackground;
public ImageAdapter(Context c) {
ctx = c;
TypedArray ta = obtainStyledAttributes(R.styleable.Gallery1);
imageBackground = ta.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 1);
ta.recycle();
}
@Override
public int getCount() {
return pics.length;
}
@Override
public Object getItem(int arg0) {
return arg0;
}
@Override
public long getItemId(int arg0) {
return arg0;
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
ImageView iv = new ImageView(ctx);
iv.setImageResource(pics[arg0]);
iv.setScaleType(ImageView.ScaleType.FIT_XY);
iv.setLayoutParams(new Gallery.LayoutParams(300,400));
iv.setBackgroundResource(imageBackground);
return iv;
}
}
}
我的第二个活动称为 MainActivity2.java 并且是空白的。请帮助。
编辑:为 MainActivity2 添加清单条目
<activity android:label="@string/app_name" android:name=".MainActivity2" >
<intent-filter>
<action android:name="AnotherActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
这是我的日志:
07-22 06:40:37.639: E/AndroidRuntime(770): FATAL EXCEPTION: main
07-22 06:40:37.639: E/AndroidRuntime(770): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.rahuldravid/com.example.rahuldravid.MainActivity2}: java.lang.RuntimeException: Binary XML file line #2: You must supply a layout_width attribute.
07-22 06:40:37.639: E/AndroidRuntime(770): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
07-22 06:40:37.639: E/AndroidRuntime(770): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-22 06:40:37.639: E/AndroidRuntime(770): at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-22 06:40:37.639: E/AndroidRuntime(770): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-22 06:40:37.639: E/AndroidRuntime(770): at android.os.Handler.dispatchMessage(Handler.java:99)
07-22 06:40:37.639: E/AndroidRuntime(770): at android.os.Looper.loop(Looper.java:137)
07-22 06:40:37.639: E/AndroidRuntime(770): at android.app.ActivityThread.main(ActivityThread.java:5041)
07-22 06:40:37.639: E/AndroidRuntime(770): at java.lang.reflect.Method.invokeNative(Native Method)
07-22 06:40:37.639: E/AndroidRuntime(770): at java.lang.reflect.Method.invoke(Method.java:511)
07-22 06:40:37.639: E/AndroidRuntime(770): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-22 06:40:37.639: E/AndroidRuntime(770): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-22 06:40:37.639: E/AndroidRuntime(770): at dalvik.system.NativeStart.main(Native Method)
07-22 06:40:37.639: E/AndroidRuntime(770): Caused by: java.lang.RuntimeException: Binary XML file line #2: You must supply a layout_width attribute.
07-22 06:40:37.639: E/AndroidRuntime(770): at android.content.res.TypedArray.getLayoutDimension(TypedArray.java:491)
07-22 06:40:37.639: E/AndroidRuntime(770): at android.view.ViewGroup$LayoutParams.setBaseAttributes(ViewGroup.java:5614)
07-22 06:40:37.639: E/AndroidRuntime(770): at android.view.ViewGroup$MarginLayoutParams.<init>(ViewGroup.java:5756)
07-22 06:40:37.639: E/AndroidRuntime(770): at android.widget.FrameLayout$LayoutParams.<init>(FrameLayout.java:610)
07-22 06:40:37.639: E/AndroidRuntime(770): at android.widget.FrameLayout.generateLayoutParams(FrameLayout.java:554)
07-22 06:40:37.639: E/AndroidRuntime(770): at android.widget.FrameLayout.generateLayoutParams(FrameLayout.java:56)
07-22 06:40:37.639: E/AndroidRuntime(770): at android.view.LayoutInflater.inflate(LayoutInflater.java:477)
07-22 06:40:37.639: E/AndroidRuntime(770): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
07-22 06:40:37.639: E/AndroidRuntime(770): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
07-22 06:40:37.639: E/AndroidRuntime(770): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
07-22 06:40:37.639: E/AndroidRuntime(770): at android.app.Activity.setContentView(Activity.java:1881)
07-22 06:40:37.639: E/AndroidRuntime(770): at com.example.rahuldravid.MainActivity2.onCreate(MainActivity2.java:13)
07-22 06:40:37.639: E/AndroidRuntime(770): at android.app.Activity.performCreate(Activity.java:5104)
07-22 06:40:37.639: E/AndroidRuntime(770): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
07-22 06:40:37.639: E/AndroidRuntime(770): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
07-22 06:40:37.639: E/AndroidRuntime(770): ... 11 more
布局1:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerHorizontal="true" >
<Gallery
android:id="@+id/Gallery01"
android:layout_width="match_parent"
android:layout_height="110dp" />
<ImageView
android:id="@+id/ImageView01"
android:layout_width="368dp"
android:layout_height="279dp"
android:layout_above="@+id/linearLayout1"
android:layout_alignParentLeft="true" />
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="68dp"
android:background="@android:drawable/bottom_bar"
android:orientation="horizontal"
android:paddingBottom="1.0dip"
android:paddingLeft="4.0dip"
android:paddingRight="4.0dip"
android:paddingTop="5.0dip" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Set Wallpaper" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="47dp"
android:layout_weight="1"
android:text="More Info" />
</LinearLayout>
</RelativeLayout>
布局2:
<?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:layout_centerHorizontal="true">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>