我是android的新手,并且在我尝试打开此Activity时使用了此代码,这给了我错误。
码:
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(本机方法)
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):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1006)
01-23 02:05:32.425:E / AndroidRuntime(20375):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
01-23 02:05:32.425:E / AndroidRuntime(20375):在dalvik.system.NativeStart.main(本地方法)
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):在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个
最佳答案
Android中有2个LoaderManager实施(支持库中一个默认实施,另一个在支持库中)。这些是:
android.app.LoaderManager
android.support.v4.app.LoaderManager
如果输入错误,则可能导致classcastexception。