错误:无法转换为android.support.v4.app.LoaderManager $ LoaderCallbacks [英] Error: cannot be cast to android.support.v4.app.LoaderManager$LoaderCallbacks
问题描述
我正在尝试在我的应用程序中实现一个搜索功能,这将是从sqlite数据库中填充的b $ b,但我遇到了挑战,当我在模拟器中测试
应用程序时我实现了搜索功能,它崩溃了
并在我的logcat中显示以下内容
引起:java.lang.ClassCastException:
com.akada.gemstouch.searchfeature.SearchableActivity
无法强制转换为android.support.v4.app.LoaderManager $ LoaderCallbacks
请参阅下面的代码
导入android.support.v4.app.LoaderManager;
导入android.support.v4.content.CursorLoader;
导入android.content.Intent;
import android.support.v4.content.Loader;
import android.database.Cursor;
import android .net.Uri;
导入android.os.Bundle;
导入android.support.v4.app.FragmentActivity;
导入android。 widget.ImageView;
导入android.widget.TextView;
$ / $
/ **
*由Akinyemi于2015年5月8日创建。
* /
公共类CountryActivity extends FragmentActivity实现了LoaderManager.LoaderCallbacks< cursor> {
私人Uri mUri;
私人ImageView mIvFlag;
私人TextView mTvName;
私有TextView mTvCurrency;
@Override
protected void onCreate(Bundle arg0){
super .onCreate(arg0);
setContentView(R.layout.activity_country);
意图intent = getIntent();
mUri = intent.getData();
mIvFlag =(ImageView)findViewById(R.id.iv_flag);
mTvName =(TextView)findViewById(R.id.tv_name);
mTvCurrency =(TextView)findViewById(R.id.tv_currency);
//在非ui线程中调用onCreateloader()方法
getSupportLoaderManager()。initLoader(0,null,this);
$
/ **由initLoader()调用* /
@Override
public Loader< ;光标> onCreateLoader(int arg0,Bundle arg1){
返回新的CursorLoader(getBaseContext(),mUri,null,null,null,null);
}
/ **由onCreateLoader()调用,将在ui-thread中执行* /
@Override
public void onLoadFinished (Loader< cursor> arg0,Cursor cursor){
if(cursor.moveToFirst()){
mIvFlag.setImageResource(cursor.getInt(cursor.getColumnIndex(cursor)。 getColumnName(2))));
mTvName.setText(Country:+ cursor.getString(cursor.getColumnIndex(cursor.getColumnName(1))));
mTvCurrency.setText(Currency:+ cursor.getString(cursor.getColumnIndex(cursor.getColumnName(3))));
}
}
@Override
public void onLoaderReset(Loader< cursor> arg0){
// TODO自动生成的方法stub
}
}
I am trying to implement a search feature in my app that will be
populated from sqlite db but am having challenges, when i test the
app in the emulator and i implemented the search feature, it crashes
and shows the following in my logcat
"Caused by: java.lang.ClassCastException:
com.akada.gemstouch.searchfeature.SearchableActivity
cannot be cast to android.support.v4.app.LoaderManager$LoaderCallbacks"
see the code below
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.content.Intent;
import android.support.v4.content.Loader;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.widget.ImageView;
import android.widget.TextView;
/**
* Created by Akinyemi on 5/8/2015.
*/
public class CountryActivity extends FragmentActivity implements LoaderManager.LoaderCallbacks<cursor> {
private Uri mUri;
private ImageView mIvFlag;
private TextView mTvName;
private TextView mTvCurrency;
@Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
setContentView(R.layout.activity_country);
Intent intent = getIntent();
mUri = intent.getData();
mIvFlag = (ImageView) findViewById(R.id.iv_flag);
mTvName = (TextView) findViewById(R.id.tv_name);
mTvCurrency = (TextView) findViewById(R.id.tv_currency);
// Invokes the method onCreateloader() in non-ui thread
getSupportLoaderManager().initLoader(0, null, this);
}
/** Invoked by initLoader() */
@Override
public Loader<cursor> onCreateLoader(int arg0, Bundle arg1) {
return new CursorLoader(getBaseContext(), mUri, null, null , null, null);
}
/** Invoked by onCreateLoader(), will be executed in ui-thread */
@Override
public void onLoadFinished(Loader<cursor> arg0, Cursor cursor) {
if(cursor.moveToFirst()){
mIvFlag.setImageResource(cursor.getInt(cursor.getColumnIndex(cursor.getColumnName(2))));
mTvName.setText("Country: "+cursor.getString(cursor.getColumnIndex(cursor.getColumnName(1))));
mTvCurrency.setText("Currency: "+cursor.getString(cursor.getColumnIndex(cursor.getColumnName(3))));
}
}
@Override
public void onLoaderReset(Loader<cursor> arg0) {
// TODO Auto-generated method stub
}
}
推荐答案
LoaderCallbacks
见下面的代码
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.content.Intent;
import android.support.v4.content .Loader;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.widget.ImageView;
import android.widget.TextView;
/ **
*由Akinyemi于2015年5月8日创建。
* /
公共类CountryActivity extends FragmentActivity实现了LoaderManager.LoaderCallbacks< cursor> {
私人Uri mUri;
私人ImageView mIvFlag;
私人TextView mTvName;
私有TextView mTvCurrency;
@Override
protected void onCreate(Bundle arg0){
super .onCreate(arg0);
setContentView(R.layout.activity_country);
意图intent = getIntent();
mUri = intent.getData();
mIvFlag =(ImageView)findViewById(R.id.iv_flag);
mTvName =(TextView)findViewById(R.id.tv_name);
mTvCurrency =(TextView)findViewById(R.id.tv_currency);
//在非ui线程中调用onCreateloader()方法
getSupportLoaderManager()。initLoader(0,null,this);
$
/ **由initLoader()调用* /
@Override
public Loader< ;光标> onCreateLoader(int arg0,Bundle arg1){
返回新的CursorLoader(getBaseContext(),mUri,null,null,null,null);
}
/ **由onCreateLoader()调用,将在ui-thread中执行* /
@Override
public void onLoadFinished (Loader< cursor> arg0,Cursor cursor){
if(cursor.moveToFirst()){
mIvFlag.setImageResource(cursor.getInt(cursor.getColumnIndex(cursor)。 getColumnName(2))));
mTvName.setText(Country:+ cursor.getString(cursor.getColumnIndex(cursor.getColumnName(1))));
mTvCurrency.setText(Currency:+ cursor.getString(cursor.getColumnIndex(cursor.getColumnName(3))));
}
}
@Override
public void onLoaderReset(Loader< cursor> arg0){
// TODO自动生成的方法stub
}
}
LoaderCallbacks"
see the code below
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.content.Intent;
import android.support.v4.content.Loader;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.widget.ImageView;
import android.widget.TextView;
/**
* Created by Akinyemi on 5/8/2015.
*/
public class CountryActivity extends FragmentActivity implements LoaderManager.LoaderCallbacks<cursor> {
private Uri mUri;
private ImageView mIvFlag;
private TextView mTvName;
private TextView mTvCurrency;
@Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
setContentView(R.layout.activity_country);
Intent intent = getIntent();
mUri = intent.getData();
mIvFlag = (ImageView) findViewById(R.id.iv_flag);
mTvName = (TextView) findViewById(R.id.tv_name);
mTvCurrency = (TextView) findViewById(R.id.tv_currency);
// Invokes the method onCreateloader() in non-ui thread
getSupportLoaderManager().initLoader(0, null, this);
}
/** Invoked by initLoader() */
@Override
public Loader<cursor> onCreateLoader(int arg0, Bundle arg1) {
return new CursorLoader(getBaseContext(), mUri, null, null , null, null);
}
/** Invoked by onCreateLoader(), will be executed in ui-thread */
@Override
public void onLoadFinished(Loader<cursor> arg0, Cursor cursor) {
if(cursor.moveToFirst()){
mIvFlag.setImageResource(cursor.getInt(cursor.getColumnIndex(cursor.getColumnName(2))));
mTvName.setText("Country: "+cursor.getString(cursor.getColumnIndex(cursor.getColumnName(1))));
mTvCurrency.setText("Currency: "+cursor.getString(cursor.getColumnIndex(cursor.getColumnName(3))));
}
}
@Override
public void onLoaderReset(Loader<cursor> arg0) {
// TODO Auto-generated method stub
}
}
这篇关于错误:无法转换为android.support.v4.app.LoaderManager $ LoaderCallbacks的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!