[编辑]
@antonio的解决方案有效。请参见下面的快照以获取证明。
我正在尝试使用JorgeCastilloPrz的AndroidFillableLoaders库,这是我第一次使用SVG(或Path)。因此,如果这个问题太幼稚,请忍受。
图书馆链接:https://github.com/JorgeCastilloPrz/AndroidFillableLoaders
代码:
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/rl_root_splash_activity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/splash_screen_color_1"
android:paddingBottom="100dp"
android:paddingEnd="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingStart="16dp">
<com.github.jorgecastillo.FillableLoader
android:id="@+id/fillableLoader"
android:layout_width="200dp"
android:layout_height="100dp"
app:fl_clippingTransform="waves"
app:fl_fillColor="#1c9ade"
app:fl_fillDuration="5000"
app:fl_originalHeight="970"
app:fl_originalWidth="970"
app:fl_strokeColor="#1c9ade"
app:fl_strokeDrawingDuration="2000"
app:fl_strokeWidth="16dp" />
</RelativeLayout>
我正在使用库中的示例应用程序中的INDOMINOUS_REX字符串作为SVG路径。链接:https://github.com/JorgeCastilloPrz/AndroidFillableLoaders/blob/master/sampleapp/src/main/java/com/github/jorgecastillo/Paths.java
MainActivity.java
public class SplashActivity extends AppCompatActivity {
RelativeLayout rlRoot;
FillableLoader fillableLoader;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
rlRoot = (RelativeLayout) findViewById(R.id.rl_root_splash_activity);
FillableLoaderBuilder loaderBuilder = new FillableLoaderBuilder();
fillableLoader = loaderBuilder
.parentView(rlRoot)
.svgPath(Const.Paths.INDOMINUS_REX)
.layoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT))
.originalDimensions(970, 970)
.strokeColor(Color.parseColor("#1c9ade"))
.fillColor(Color.parseColor("#1c9ade"))
.strokeDrawingDuration(2000)
.clippingTransform(new WavesClippingTransform())
.fillDuration(10000)
.build();
}
}
当我运行代码时,出现以下异常:
java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
at com.github.jorgecastillo.svg.SvgPathParser.parsePath(SvgPathParser.java:52)
at com.github.jorgecastillo.FillableLoader.buildPathData(FillableLoader.java:374)
at com.github.jorgecastillo.FillableLoader.onSizeChanged(FillableLoader.java:224)
at android.view.View.sizeChange(View.java:16748)
at android.view.View.setFrame(View.java:16710)
at android.view.View.layout(View.java:16627)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1079)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
at com.android.internal.policy.PhoneWindow$DecorView.onLayout(PhoneWindow.java:2678)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2171)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1931)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
at android.view.Choreographer.doCallbacks(Choreographer.java:670)
at android.view.Choreographer.doFrame(Choreographer.java:606)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
有人可以帮我解决这个问题吗?
@antonio的解决方案有效,这就是证明...
最佳答案
发生NPE的原因是您在com.github.jorgecastillo.FillableLoader
中定义了R.layout.activity_splash
,但尚未以编程方式设置生成的路径。从the documentation
为了解决这个问题,您的onCreate
可以像这样:
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
rlRoot = (RelativeLayout) findViewById(R.id.rl_root_splash_activity);
fillableLoader = (FillableLoader) findViewById(R.id.fillableLoader);
fillableLoader.setSvgPath(Const.Paths.INDOMINUS_REX);
}
请注意,您已经在布局和编程中都定义了一个FillableLoader
,但是导致NPE的定义在布局中是一个,因为它缺少setSvgPath
。另一种选择是在使用
FillableLoader
以编程方式生成布局时,从布局中删除FillableLoaderBuilder
定义:activity_splash.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/rl_root_splash_activity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/splash_screen_color_1"
android:paddingBottom="100dp"
android:paddingEnd="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingStart="16dp">
</RelativeLayout>
SplashActivity.onCreate方法@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rlRoot = (RelativeLayout) findViewById(R.id.rl_root_splash_activity);
FillableLoaderBuilder loaderBuilder = new FillableLoaderBuilder();
fillableLoader = loaderBuilder
.parentView(rlRoot)
.svgPath(Const.Paths.INDOMINUS_REX)
.layoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT))
.originalDimensions(970, 970)
.strokeColor(Color.parseColor("#1c9ade"))
.fillColor(Color.parseColor("#1c9ade"))
.strokeDrawingDuration(2000)
.clippingTransform(new WavesClippingTransform())
.fillDuration(10000)
.build();
fillableLoader.setSvgPath(Paths.INDOMINUS_REX);
}
请注意,在两种情况下,您都需要调用
fillableLoader.start();
来观看动画。