Android 为什么我在这个项目中得到一个NullPointerException?
既然这个问题已经解决了,我将解决剩下的问题。这是我的工作代码:Android 为什么我在这个项目中得到一个NullPointerException?,android,android-fragments,android-animation,android-tabs,tween,Android,Android Fragments,Android Animation,Android Tabs,Tween,既然这个问题已经解决了,我将解决剩下的问题。这是我的工作代码: package edu.wmich.lab4_jjohns1119; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.B
package edu.wmich.lab4_jjohns1119;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.view.animation.AnimationUtils;
import android.view.animation.Animation;
public class MainFragment extends Fragment {
//Declare widget objects
Button btnAnimate;
ImageView imgTween;
Animation tweenAnimation;
//create view
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//Inflater
View rootView = inflater.inflate(R.layout.fragment_main, container,false);
//Button
btnAnimate = (Button)rootView.findViewById(R.id.btnAnimate);
//Image
imgTween = (ImageView)rootView.findViewById(R.id.imgTween);
//Animation Resource
tweenAnimation = AnimationUtils.loadAnimation(getActivity(), R.anim.tween1);
//Button listener for animation
btnAnimate.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//animate image
imgTween.startAnimation(tweenAnimation);
}
});
//Return view
return rootView;
}
}
它启动、导航,但动画什么也不做。我猜代码的顺序不正确,因为它在动画之前显示视图。以下是我的tween1.xml:
<?xml version="1.0" encoding="utf-8"?>
<rotate
xmlns:android="http//schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:toDegrees="-45"
android:duration="400"
/>
您的问题在于分配
tweenAnimation
字段
Animation tweenAnimation = AnimationUtils.loadAnimation(getActivity(), R.anim.tween1);
getActivity()
在片段生命周期(创建)的这一点上为空,并将导致loadAnimation
失败或返回空动画。访问此项将导致NPE(如果loadAnimation
尚未抛出NPE)
将分配tweenimation
移动到片段生命周期中活动将存在的点,如onCreate
前
您的问题在于分配
tweenAnimation
字段
Animation tweenAnimation = AnimationUtils.loadAnimation(getActivity(), R.anim.tween1);
getActivity()
在片段生命周期(创建)的这一点上为空,并将导致loadAnimation
失败或返回空动画。访问此项将导致NPE(如果loadAnimation
尚未抛出NPE)
将分配tweenimation
移动到片段生命周期中活动将存在的点,如onCreate
前
问题是,在活动完成构建之前,不能调用
getView()
或getActivity()
。因此,您只需要在调用这些方法之后,将onActivityCreated(…)
方法覆盖 问题在于,在活动完成构建之前,不能调用getView()
或getActivity()
。因此,您只需要在调用这些方法之后,将onActivityCreated(…)
方法覆盖 为什么要使用getView()?
将所有工作分配移动到rootView下方,然后执行以下操作:
btnAnimate = (Button)rootView.findViewById(R.id.btnAnimate);
要使动画正常工作,请将命名空间声明更改为:
http://schemas.android.com/apk/res/android
(请注意,示例中缺少http后面的冒号)
..为什么要使用getView()?
将所有工作分配移动到rootView下方,然后执行以下操作:
btnAnimate = (Button)rootView.findViewById(R.id.btnAnimate);
要使动画正常工作,请将命名空间声明更改为:
http://schemas.android.com/apk/res/android
(请注意,示例中缺少http后面的冒号)
..发布堆栈跟踪。然后我们可以看到抛出异常的行。请原谅我的无知,但我似乎不知道如何获取堆栈跟踪。你是说logcat输出吗?@pfortyseven是的,logcat输出。似乎是我声明按钮的代码行……但是为什么?找到了!xml文件中的命名空间错误。在“http”之后添加“:”。发布堆栈跟踪。然后我们可以看到抛出异常的行。请原谅我的无知,但我似乎不知道如何获取堆栈跟踪。你是说logcat输出吗?@pfortyseven是的,logcat输出。似乎是我声明按钮的代码行……但是为什么?找到了!xml文件中的命名空间错误。在“http”后面加“:”。观察得很好。我错过了。请看我的编辑,还有一个问题,Logcat仍然会有帮助。@KieronPapps,你明白了。刚刚发表了很好的观察。我错过了。请看我的编辑,还有一个问题,Logcat仍然会有帮助。@KieronPapps,你明白了。刚刚发帖谢谢!!!不要再撞车了。现在,我想知道为什么动画按钮对图像没有任何作用……我需要在tween1.xml中对其进行更改,对吗?不客气。不太清楚动画。也许tween1.xml缺少一些东西。只是发布了它@约瑟森谢谢你!!!不要再撞车了。现在,我想知道为什么动画按钮对图像没有任何作用……我需要在tween1.xml中对其进行更改,对吗?不客气。不太清楚动画。也许tween1.xml缺少一些东西。只是发布了它@何塞