我是android中的新手并使用此代码,而当我尝试打开此活动时,它给了我错误。 的 CODE:
public class LoginActivityService extends Activity implements LoaderManager.LoaderCallbacks<List<User>>
{ Context context;
EditText userName,password,version;
Button logoutBtn;
LoginLoader loginLoader;
ImageButton loginBtn;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.login_view_service);
context = this;
userName = (EditText)findViewById(R.id.userName1);
password = (EditText)findViewById(R.id.password1);
loginBtn = (ImageButton)findViewById(R.id.login1);
logoutBtn = (Button)findViewById(R.id.logout1);
loginBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(userName.getText().length() == 0 || password.getText().length() == 0)
{
Toast.makeText(context,"UserName Or Password Should be Filled",Toast.LENGTH_SHORT).show();
}
else
{
userName.setText("");
password.setText("");
//HOW CAN I WRITE BELOW LINE INSIDE { }
}
}
});
getLoaderManager().initLoader(1, null, (LoaderCallbacks<List<User>>) this);
}
@Override
public Loader<List<User>> onCreateLoader(int id, Bundle args) {
loginLoader = new LoginLoader(context,userName.getText().toString(),password.getText().toString(),"2013-07-10 01:18:26");
/* args.putString("username",userName.getText().toString());
args.putString("password",password.getText().toString());
args.putString("apkStatusDate","2013-07-10 01:18:26");*/
return loginLoader;
}
@Override
public void onLoadFinished(Loader<List<User>> arg0, List<User> proposals) {
Log.i("User",""+proposals.size());
}
@Override
public void onLoaderReset(Loader<List<User>> arg0) {}
}
启动时如果此活动我收到错误:
01-23 02:05:32.425:E / AndroidRuntime(20375):致命异常:主要 01-23 02:05:32.425:E / AndroidRuntime(20375):java.lang.RuntimeException:无法启动活动ComponentInfo {com.mrfs.android.surveyapp / com.mrfs.android.surveyapp.activities.LoginActivityService}:java .lang.ClassCastException:com.mrfs.android.surveyapp.activities.LoginActivityService无法强制转换为android.app.LoaderManager $ LoaderCallbacks 01-23 02:05:32.425:E / AndroidRuntime(20375):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100) 01-23 02:05:32.425:E / AndroidRuntime(20375):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125) 01-23 02:05:32.425:E / AndroidRuntime(20375):在android.app.ActivityThread.access $ 600(ActivityThread.java:140) 01-23 02:05:32.425:E / AndroidRuntime(20375):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1227) 01-23 02:05:32.425:E / AndroidRuntime(20375):在android.os.Handler.dispatchMessage(Handler.java:99) 01-23 02:05:32.425:E / AndroidRuntime(20375):在android.os.Looper.loop(Looper.java:137) 01-23 02:05:32.425:E / AndroidRuntime(20375):在android.app.ActivityThread.main(ActivityThread.java:4898) 01-23 02:05:32.425:E / AndroidRuntime(20375):at java.lang.reflect.Method.invokeNative(Native Method) 01-23 02:05:32.425:E / AndroidRuntime(20375):at java.lang.reflect.Method.invoke(Method.java:511) 01-23 02:05:32.425:E / AndroidRuntime(20375):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1006) 01-23 02:05:32.425:E / AndroidRuntime(20375):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773) 01-23 02:05:32.425:E / AndroidRuntime(20375):at dalvik.system.NativeStart.main(Native Method) 01-23 02:05:32.425:E / AndroidRuntime(20375):引起:java.lang.ClassCastException:com.mrfs.android.surveyapp.activities.LoginActivityService无法强制转换为android.app.LoaderManager $ LoaderCallbacks 01-23 02:05:32.425:E / AndroidRuntime(20375):at com.mrfs.android.surveyapp.activities.LoginActivityService.onCreate(LoginActivityService.java:54) 01-23 02:05:32.425:E / AndroidRuntime(20375):在android.app.Activity.performCreate(Activity.java:5206) 01-23 02:05:32.425:E / AndroidRuntime(20375):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083) 01-23 02:05:32.425:E / AndroidRuntime(20375):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064) 01-23 02:05:32.425:E / AndroidRuntime(20375):... 11更多
答案 0 :(得分:0)
Android中有2个LoaderManager实现(一个默认,另一个在支持库中)。这些是:
android.app.LoaderManager
android.support.v4.app.LoaderManager
如果包含错误,则可能导致classcastexception。
答案 1 :(得分:0)
尝试使用以下命令启动加载程序:
getLoaderManager().initLoader(1, null, this);
您的活动实施LoaderCallbacks<List<User>>
,因此您只需将'this'作为参数传递,无需进行投射。