2016-03-22 11 views
-3

を起動しようとします。私はアンドロイドのjava.lang.NullPointerException:私はCubeLayoutクラスで<br> <a href="https://github.com/justomiguel/AndroidCubeDemo" rel="nofollow">Android Cube Demo</a></p> <p>を使用していインタフェースメソッド

パブリッククラスCubeLayout CubeCompletedインタフェースを追加することは、私の活動にでframeLayout {

private BaseInterpolator mInterpolator = new AccelerateDecelerateInterpolator(); 

public CubeLayout(Context context) { 
    this(context, null); 
} 

public CubeLayout(Context context, AttributeSet attrs, int defStyleAttr) { 
    super(context, attrs, defStyleAttr); 
} 

public CubeLayout(Context context, AttributeSet attrs) { 
    this(context, attrs, 0); 
} 

@Override 
protected void onFinishInflate() { 
    super.onFinishInflate(); 
    View foregroundView = getChildAt(0); 
    View backgroundView = getChildAt(1); 

    CubeLeftOutAnimation cubeLeftOutAnimation = new CubeLeftOutAnimation(); 
    cubeLeftOutAnimation.setDuration(1000); 
    cubeLeftOutAnimation.setFillAfter(true); 

    CubeRightInAnimation cubeRightInAnimation = new CubeRightInAnimation(); 
    cubeRightInAnimation.setDuration(1000); 
    cubeRightInAnimation.setFillAfter(true); 

    foregroundView.startAnimation(cubeLeftOutAnimation); 
    backgroundView.startAnimation(cubeRightInAnimation); 

    cubeRightInAnimation.setAnimationListener(new Animation.AnimationListener() { 
     @Override 
     public void onAnimationStart(Animation animation) { 

     } 

     @Override 
     public void onAnimationEnd(Animation animation) { 
      completed.completedCube(); 

     } 

     @Override 
     public void onAnimationRepeat(Animation animation) { 

     } 
    }); 

} 
public interface CubeCompleted { 
    public void completedCube(); 
} 

CubeCompleted completed; 

} 

を拡張します。私は断片を置き換えました。

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    FragmentManager manager = getSupportFragmentManager(); 
    manager.beginTransaction() 
      .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN) 
      .replace(R.id.content_main, mHomeFragment) 
      .commit(); 

} 

MyFragmentにあります。私はcompletedCubeが

public class HomeFragment extends Fragment implements CubeLayout.CubeCompleted { 

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

    return view; 
} 

@Override 
public void completedCube() { 
    // donutProgress(); 
} 
} 

これは、あなたはそれ上の任意のメソッドを呼び出す前に、あなたのcompletedオブジェクトを初期化する必要がLogcat

Process: com.seesaa.newsaudiocast, PID: 31189 

                    java.lang.NullPointerException: Attempt to invoke interface method 'void com.view.CubeLayout$CubeCompleted.completedCube()' on a null object reference 
at com.seesaa.newsaudiocast.view.CubeLayout$1.onAnimationEnd(CubeLayout.java:56) 
at android.view.animation.Animation$3.run(Animation.java:374) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:155) 
at android.app.ActivityThread.main(ActivityThread.java:5696) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823) 
+0

あなたはそのクラスで完成したオブジェクトを初期化しましたか?私がそのコードで見たように、初期化されていません。初期化して、一度onAnimationEnd()メソッドでヌルチェックをしてみてください – Raghavendra

+0

これは私のすべてのコードです。お願いします。私はそれを修正するのに役立ちます! –

+0

おそらく 'CubeCompleted completed;を初期化する必要があります。 –

答えて

1

編集:私はあなたには、いくつかの種類のコールバックを実装するので、これを試してみるしようとしていると思う - findViewByIdを使用し、あなたのフラグメントで

public void setCallBack(CubeCompleted completed) { 
    this.completed = completed; 
} 

を -

をごCubeLayoutクラスでは、これを追加あなたのCubeLayoutを見つけ、それにリスナーを添付する -

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

    CubeLayout cubelayout = view.findViewById(R.id.cubeLayout); 
    cubeLayout.setCallBack(this); 

    return view; 
} 
+0

私は試しました。それはバグではありません。しかし、MyFragmentで。私がcompletedCudeをオーバーライドするとき。それは動作しません。 @Override public void completedCube(){ Toast.make(....); } –

+0

それは働いた。どうもありがとうございます! –

0

の私のバグであるオーバライド。

+0

どうすればいいですか? 。コードを教えてもらえますか? –

+1

CubeLayoutがHomeFragmentで拡張されている場合は、 'findViewById'を使用してインスタンスを取得し、セッターを使用してリスナーを設定できます。 –

0
public class HomeFragment extends Fragment implements CubeLayout.CubeCompleted { 
private CubeCompleted cubeCompletedInterface; 
@Override 
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
    final View view = inflater.inflate(R.layout.fragment_home, container, false); 
    cubeCompletedInterface=getActivity(); 
    return view; 
} 

@Override 
public void completedCube() { 
    // donutProgress(); 
} 
} 
を10

/*あなたのアニメーションクラスremove CubeCompletedが完了しました。 */

private BaseInterpolator mInterpolator = new AccelerateDecelerateInterpolator(); 

public CubeLayout(Context context) { 
    this(context, null); 
} 

public CubeLayout(Context context, AttributeSet attrs, int defStyleAttr) { 
    super(context, attrs, defStyleAttr); 
} 

public CubeLayout(Context context, AttributeSet attrs) { 
    this(context, attrs, 0); 
} 

@Override 
protected void onFinishInflate() { 
    super.onFinishInflate(); 
    View foregroundView = getChildAt(0); 
    View backgroundView = getChildAt(1); 

    CubeLeftOutAnimation cubeLeftOutAnimation = new CubeLeftOutAnimation(); 
    cubeLeftOutAnimation.setDuration(1000); 
    cubeLeftOutAnimation.setFillAfter(true); 

    CubeRightInAnimation cubeRightInAnimation = new CubeRightInAnimation(); 
    cubeRightInAnimation.setDuration(1000); 
    cubeRightInAnimation.setFillAfter(true); 

    foregroundView.startAnimation(cubeLeftOutAnimation); 
    backgroundView.startAnimation(cubeRightInAnimation); 

    cubeRightInAnimation.setAnimationListener(new Animation.AnimationListener() { 
     @Override 
     public void onAnimationStart(Animation animation) { 

     } 

     @Override 
     public void onAnimationEnd(Animation animation) { 
      completed.completedCube(); 

     } 

     @Override 
     public void onAnimationRepeat(Animation animation) { 

     } 
    }); 

} 
public interface CubeCompleted { 
    public void completedCube(); 
} 



} 
+0

完了したCubeCompletedを削除します。 なぜですか? @Override public void onAnimationEnd(アニメーションアニメーション){ が完了しました。completedCube(); } –

+0

私はHomeFragmentから初期化できませんか? –

+0

'CubeLayout'クラスには、' completed'というオブジェクトが定義されていません。この呼び出しはどのように 'completed.completedCube();'になっていますか? – Rohan

関連する問題

 関連する問題