PhysicsRelativeLayout:NullPointerException导致setGravity(...)到PhysicsRelativeLayout

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PhysicsRelativeLayout:NullPointerException导致setGravity(...)到PhysicsRelativeLayout相关的知识,希望对你有一定的参考价值。

我正在使用library试验这个PhysicsRelativeLayout如下:

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/layout_simulation"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/layout_top"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/button_physics_reset"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </LinearLayout>

    <com.jawnnypoo.physicslayout.PhysicsRelativeLayout
            android:id="@+id/physics_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:boundsSize="100dp">

            <ImageView
                android:id="@+id/iv_one"
                android:layout_width="110dp"
                android:layout_height="100dp"
                android:src="@drawable/bg_one"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true"
                android:layout_marginLeft="48dp"
                android:layout_marginTop="48dp" />

            <ImageView
                android:id="@+id/iv_two"
                android:layout_width="110dp"
                android:layout_height="100dp"
                android:src="@drawable/bg_two"
                android:layout_alignParentRight="true"
                android:layout_alignParentTop="true"
                android:layout_marginRight="48dp"
                android:layout_marginTop="48dp" />
    </com.jawnnypoo.physicslayout.PhysicsRelativeLayout>
  </LinearLayout>

Java的:

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jawnnypoo.physicslayout.PhysicsRelativeLayout;
import com.experiment.physicslayout.R;

public class SimulationFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_simulation, container, false);

        PhysicsRelativeLayout physicsRelativeLayout = (PhysicsRelativeLayout) view.findViewById(R.id.physics_layout);
        physicsRelativeLayout.getPhysics().enablePhysics(); // ok
        physicsRelativeLayout.getPhysics().enableFling(); // ok

        physicsRelativeLayout.getPhysics().setGravity(0.00f, 24.8f); // NPE
        return view;
    }
}

当我试图将引力设置如下:

physicsRelativeLayout.getPhysics().setGravity(0.00f, 24.8f);

它会导致以下错误:

Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'void org.jbox2d.dynamics.World.setGravity(org.jbox2d.common.Vec2)' on a null object reference
       at com.jawnnypoo.physicslayout.Physics.setGravity(Physics.java:686)
       at com.jawnnypoo.physicslayout.Physics.setGravityX(Physics.java:661)
       at com.experiment.physicslayout.ui.fragment.SimulationFragment.onCreateView(SimulationFragment.java:43)
       at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974)
       at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
 ..........
 ..........
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

任何的想法?提前致谢!

答案

只需使用try case handler

            try {

                physicsLayout.getPhysics().setGravity(-event.values[0], event.values[1]);

            } catch (Exception e) {
                e.printStackTrace();
            }

以上是关于PhysicsRelativeLayout:NullPointerException导致setGravity(...)到PhysicsRelativeLayout的主要内容,如果未能解决你的问题,请参考以下文章

删除“NUL”字符

NUL 与 NULL

比较字符串忽略 NUL

Java套接字在写入字节数组后仅获得NUL字符

如何从Java中的字符串中删除nul字符(\ 0)

屏蔽回显