我决定尝试为我放置在背景图像上的船对象(imageView)制作单独的布局。
这样做的原因是,我希望能够在单击人时将人对象附加到船对象。
这样,当单击船对象时,人对象会随之移动。我曾尝试根据我在网上找到的内容以编程方式编写添加布局的代码,但应用程序不断崩溃。
以下是主要活动:
package com.cannibal_photographer;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.FrameLayout;
import android.widget.FrameLayout.LayoutParams;
import android.widget.RelativeLayout;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FrameLayout boatlayout = new FrameLayout(this);
Boat boatobject = (Boat)findViewById(R.id.boatimageView);
//Person personobject = (Person)findViewById(R.id.personView1);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
boatlayout.addView(boatobject, params);
RelativeLayout.LayoutParams parentParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
this.addContentView(boatlayout,parentParams);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
这是主要布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@layout/activity_main"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<com.cannibal_photographer.Person
android:id="@+id/personView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="250dp"
android:gravity="center_vertical"
android:src="@drawable/greensquare" />
</RelativeLayout>
这是boatlayout.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/framelayoutView"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.cannibal_photographer.Boat
android:id="@+id/boatimageView"
android:layout_width="78dp"
android:layout_height="130dp"
android:layout_marginLeft="105dp"
android:layout_marginTop="180dp"
android:src="@drawable/boat" />
</FrameLayout>
最后是堆栈跟踪:
10-17 08:50:03.761: E/AndroidRuntime(2283): FATAL EXCEPTION: main
10-17 08:50:03.761: E/AndroidRuntime(2283): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cannibal_photographer/com.cannibal_photographer.MainActivity}: java.lang.NullPointerException
10-17 08:50:03.761: E/AndroidRuntime(2283): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
10-17 08:50:03.761: E/AndroidRuntime(2283): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
10-17 08:50:03.761: E/AndroidRuntime(2283): at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-17 08:50:03.761: E/AndroidRuntime(2283): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
10-17 08:50:03.761: E/AndroidRuntime(2283): at android.os.Handler.dispatchMessage(Handler.java:99)
10-17 08:50:03.761: E/AndroidRuntime(2283): at android.os.Looper.loop(Looper.java:137)
10-17 08:50:03.761: E/AndroidRuntime(2283): at android.app.ActivityThread.main(ActivityThread.java:5041)
10-17 08:50:03.761: E/AndroidRuntime(2283): at java.lang.reflect.Method.invokeNative(Native Method)
10-17 08:50:03.761: E/AndroidRuntime(2283): at java.lang.reflect.Method.invoke(Method.java:511)
10-17 08:50:03.761: E/AndroidRuntime(2283): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
10-17 08:50:03.761: E/AndroidRuntime(2283): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
10-17 08:50:03.761: E/AndroidRuntime(2283): at dalvik.system.NativeStart.main(Native Method)
10-17 08:50:03.761: E/AndroidRuntime(2283): Caused by: java.lang.NullPointerException
10-17 08:50:03.761: E/AndroidRuntime(2283): at android.view.ViewGroup.addViewInner(ViewGroup.java:3338)
10-17 08:50:03.761: E/AndroidRuntime(2283): at android.view.ViewGroup.addView(ViewGroup.java:3210)
10-17 08:50:03.761: E/AndroidRuntime(2283): at android.view.ViewGroup.addView(ViewGroup.java:3186)
10-17 08:50:03.761: E/AndroidRuntime(2283): at com.cannibal_photographer.MainActivity.onCreate(MainActivity.java:20)
10-17 08:50:03.761: E/AndroidRuntime(2283): at android.app.Activity.performCreate(Activity.java:5104)
10-17 08:50:03.761: E/AndroidRuntime(2283): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
10-17 08:50:03.761: E/AndroidRuntime(2283): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
10-17 08:50:03.761: E/AndroidRuntime(2283): ... 11 more
10-17 08:53:04.651: E/Trace(2381): error opening trace file: No such file or directory (2)
10-17 08:53:05.611: D/dalvikvm(2381): GC_FOR_ALLOC freed 37K, 6% free 2556K/2712K, paused 96ms, total 98ms
10-17 08:53:05.631: I/dalvikvm-heap(2381): Grow heap (frag case) to 5.245MB for 2764816-byte allocation
10-17 08:53:05.681: D/dalvikvm(2381): GC_FOR_ALLOC freed 2K, 3% free 5254K/5416K, paused 45ms, total 45ms
10-17 08:53:05.731: D/dalvikvm(2381): GC_CONCURRENT freed <1K, 3% free 5264K/5416K, paused 4ms+14ms, total 50ms
10-17 08:53:05.731: D/AndroidRuntime(2381): Shutting down VM
10-17 08:53:05.731: W/dalvikvm(2381): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
10-17 08:53:05.741: E/AndroidRuntime(2381): FATAL EXCEPTION: main
10-17 08:53:05.741: E/AndroidRuntime(2381): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cannibal_photographer/com.cannibal_photographer.MainActivity}: java.lang.NullPointerException
10-17 08:53:05.741: E/AndroidRuntime(2381): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
10-17 08:53:05.741: E/AndroidRuntime(2381): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
10-17 08:53:05.741: E/AndroidRuntime(2381): at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-17 08:53:05.741: E/AndroidRuntime(2381): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
10-17 08:53:05.741: E/AndroidRuntime(2381): at android.os.Handler.dispatchMessage(Handler.java:99)
10-17 08:53:05.741: E/AndroidRuntime(2381): at android.os.Looper.loop(Looper.java:137)
10-17 08:53:05.741: E/AndroidRuntime(2381): at android.app.ActivityThread.main(ActivityThread.java:5041)
10-17 08:53:05.741: E/AndroidRuntime(2381): at java.lang.reflect.Method.invokeNative(Native Method)
10-17 08:53:05.741: E/AndroidRuntime(2381): at java.lang.reflect.Method.invoke(Method.java:511)
10-17 08:53:05.741: E/AndroidRuntime(2381): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
10-17 08:53:05.741: E/AndroidRuntime(2381): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
10-17 08:53:05.741: E/AndroidRuntime(2381): at dalvik.system.NativeStart.main(Native Method)
10-17 08:53:05.741: E/AndroidRuntime(2381): Caused by: java.lang.NullPointerException
10-17 08:53:05.741: E/AndroidRuntime(2381): at android.view.ViewGroup.addViewInner(ViewGroup.java:3338)
10-17 08:53:05.741: E/AndroidRuntime(2381): at android.view.ViewGroup.addView(ViewGroup.java:3210)
10-17 08:53:05.741: E/AndroidRuntime(2381): at android.view.ViewGroup.addView(ViewGroup.java:3186)
10-17 08:53:05.741: E/AndroidRuntime(2381): at com.cannibal_photographer.MainActivity.onCreate(MainActivity.java:20)
10-17 08:53:05.741: E/AndroidRuntime(2381): at android.app.Activity.performCreate(Activity.java:5104)
10-17 08:53:05.741: E/AndroidRuntime(2381): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
10-17 08:53:05.741: E/AndroidRuntime(2381): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
10-17 08:53:05.741: E/AndroidRuntime(2381): ... 11 more
更新:
Carnal 是对的,我找到了帮助解决问题的文档:
http://khajanpndey.blogspot.com/2012/12/android-layoutinflater-tutorial.html