我正在从这个site做ListView示例。以下是我的代码,
public class MainActivity extends Activity
{
private ListView listView;
String[] values = new String[] { "A","B","C","D","E","F","G","H","I","J" };
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listData);
}
@Override
protected void onResume()
{
super.onResume();
final ArrayList<String> list = new ArrayList<String>();
for ( int i = 0 ; i < values.length ; i++ )
{
list.add(values[i]);
}
final MyAdapter adapter = new MyAdapter ( this, android.R.layout.simple_list_item_1, list );
listView.setAdapter(adapter);
listView.setOnItemClickListener( new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick ( AdapterView<?> parent, final View view, int position, long id )
{
final String item = ( String ) parent.getItemAtPosition(position);
view.animate().setDuration(2000).alpha(0)
.withEndAction( new Runnable()
{
@Override
public void run()
{
list.remove(item);
adapter.notifyDataSetChanged();
view.setAlpha(1);
}
});
}
});
}
private class MyAdapter extends ArrayAdapter<String>
{
HashMap<String,Integer> hashMap = new HashMap<String,Integer>();
public MyAdapter( Context context, int id, List<String> list )
{
super ( context, id, list );
for ( int i = 0 ; i < list.size() ; i++ )
{
hashMap.put( list.get(i), i );
}
}
@Override
public long getItemId( int position )
{
String item = getItem(position);
return Long.valueOf(hashMap.get(item));
}
@Override
public boolean hasStableIds()
{
return true;
}
}
}
但是,当我运行此代码并单击列表视图时,它会出错。以下是我的logcat。
11-29 16:24:49.249: E/AndroidRuntime(21837): FATAL EXCEPTION: main
11-29 16:24:49.249: E/AndroidRuntime(21837): java.lang.NoSuchMethodError: android.view.ViewPropertyAnimator.withEndAction
11-29 16:24:49.249: E/AndroidRuntime(21837): at com.example.listviewexample.MainActivity$1.onItemClick(MainActivity.java:52)
11-29 16:24:49.249: E/AndroidRuntime(21837): at android.widget.AdapterView.performItemClick(AdapterView.java:292)
11-29 16:24:49.249: E/AndroidRuntime(21837): at android.widget.AbsListView.performItemClick(AbsListView.java:1394)
11-29 16:24:49.249: E/AndroidRuntime(21837): at android.widget.AbsListView$PerformClick.run(AbsListView.java:3024)
11-29 16:24:49.249: E/AndroidRuntime(21837): at android.widget.AbsListView$1.run(AbsListView.java:3830)
11-29 16:24:49.249: E/AndroidRuntime(21837): at android.os.Handler.handleCallback(Handler.java:605)
11-29 16:24:49.249: E/AndroidRuntime(21837): at android.os.Handler.dispatchMessage(Handler.java:92)
11-29 16:24:49.249: E/AndroidRuntime(21837): at android.os.Looper.loop(Looper.java:137)
11-29 16:24:49.249: E/AndroidRuntime(21837): at android.app.ActivityThread.main(ActivityThread.java:4517)
11-29 16:24:49.249: E/AndroidRuntime(21837): at java.lang.reflect.Method.invokeNative(Native Method)
11-29 16:24:49.249: E/AndroidRuntime(21837): at java.lang.reflect.Method.invoke(Method.java:511)
11-29 16:24:49.249: E/AndroidRuntime(21837): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
11-29 16:24:49.249: E/AndroidRuntime(21837): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
11-29 16:24:49.249: E/AndroidRuntime(21837): at dalvik.system.NativeStart.main(Native Method)
如何解决此错误?
答案 0 :(得分:3)
public ViewPropertyAnimator withEndAction (Runnable runnable)
此方法仅适用于API 16 ,因此,如果您的设备具有API&lt; 16,不幸的是,它不会起作用。请看Android docs。
您需要确保只有API&gt;才能执行此方法 16 强>:
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN) {
// use method
}
else {
// you can't use that method
}