尝试将多个布局堆叠到Android应用程序中的活动中,不断崩溃

时间:2013-10-17 09:02:54

标签: java android eclipse imageview android-framelayout

我决定尝试为我放在背景图像上的船对象(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

2 个答案:

答案 0 :(得分:0)

Boat boatobject = (Boat)findViewById(R.id.boatimageView);
由于您在布局null =&gt;中寻找boatobject

会在R.id.boatimageView上为您activity_main提供setContentView(R.layout.activity_main);

由于您有两个不同的布局文件,因此需要使用LayoutInflater来扩充第二个布局boatlayout.xml。您使用View获得的LayoutInflater来自Boat对象:

Boat boatobject = (Boat) viewFromSecondXml.findViewById(R.id.boatimageView);

答案 1 :(得分:0)

当您在acitivity_main中夸大MainActivity布局时,您应该在Person中初始化Boat视图而非onCreate()视图。它总是会给你Nullpointer异常,因为Boat布局中没有定义activity_main视图。

因此,除了使用Boat尝试访问Person视图如下:

    setContentView(R.layout.activity_main); 
    FrameLayout boatlayout = new FrameLayout(this);
    Person personobject = (Person)findViewById(R.id.personView1);