我编写了这段代码但是它给出了致命的异常,我处理了所有异常,这是什么原因。以及如何在imageview上应用动画?
主要活动
public class AnimViewActivity extends Activity {
/** Called when the activity is first created. */
Button b;
ImageView iv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b=(Button)findViewById(R.id.but);
try{
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
iv=(ImageView)findViewById(R.id.im);
iv.setVisibility(ImageView.VISIBLE);
iv.setBackgroundResource(R.drawable.ic_launcher);
iv.startAnimation(new Anmatam());
}
});
}
catch(Exception e){
e.printStackTrace();
}
}
}
动画
public class Anmatam extends Animation {
float centerx,centery;
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
// TODO Auto-generated method stub
super.applyTransformation(interpolatedTime, t);
try{
Matrix mt=t.getMatrix();
mt.setScale(interpolatedTime, interpolatedTime);
mt.preTranslate(-centerx,-centery);
mt.postTranslate(centerx, centery);
}
catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
@Override
public void initialize(int width, int height, int parentWidth,
int parentHeight) {
// TODO Auto-generated method stub
super.initialize(width, height, parentWidth, parentHeight);
try{
centerx=width/0.2f;
centery=height/0.2f;
setDuration(2000);
setFillAfter(true);
setInterpolator(new LinearInterpolator());
}
catch(Exception e){
e.printStackTrace();
}
}
}
清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.my.net.pro"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".AnimViewActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Anmatam">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
答案 0 :(得分:0)
你没有抓住潜在的NullPointerException
:iv.setVisibility(ImageView.VISIBLE);
。
如果在上一行找不到ImageView
,可能会发生