我正在尝试在 Android 中管理会话。我正在使用共享首选项来存储用户数据。当任何用户完美登录并且我的应用程序的主页现在应该打开时,就会出现问题,但是 Logcat 显示没有活动来处理此页面的错误。
这是 Login.java 的代码。
public class Login extends Activity {
Button but;
TextView tex,tex2;
EditText nameEdit,passEdit;
public static final String MyPREFERENCES = "MyPrefs" ;
public static final String name = "nameKey";
public static final String pass = "passwordKey";
SharedPreferences sharedpreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
but=(Button)findViewById(R.id.login);
// nameEdit, passEdit get the user entered username and password respectively.
nameEdit = (EditText)findViewById(R.id.editText1);
passEdit = (EditText)findViewById(R.id.editText2);
but.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
//storing userdetails in Shared Preference
Editor editor = sharedpreferences.edit();
String u = nameEdit.getText().toString();
String p = passEdit.getText().toString();
editor.putString(name, u);
editor.putString(pass, p);
editor.commit();
Intent openStartingPoint=new Intent("app.something.com.Menu");
startActivity(openStartingPoint); //here occurs the error no activity found to handle this
finish();
return;
}
});
}
@Override
protected void onResume() {
//if user already logged in then direct him to "Menu".
sharedpreferences=getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
if (sharedpreferences.contains(name))
{
if(sharedpreferences.contains(pass)){
Intent i = new Intent("app.something.com.Menu");
startActivity(i);
}
}
super.onResume();
}
}
注销用户的方法如下,其中共享首选项编辑器被清除
public void logout()
{
SharedPreferences sharedpreferences = getSharedPreferences(Login.MyPREFERENCES,Context.MODE_PRIVATE);
Editor editor = sharedpreferences.edit();
editor.clear();
editor.commit();
moveTaskToBack(true);
Control_panel.this.finish();
return;
}
这是清单代码:
<activity
android:screenOrientation="portrait"
android:name="app.something.com.Login"
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="app.something.com.Menu"
android:label="@string/app_name" >
<intent-filter>
<action android:name="app.something.com.Menu" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
我不知道为什么我会得到 ActivityNotFoundException,即使文件 Menu.java 扩展了 Activity 类并且在 Manifest 中也有提及。在此运行时异常之后,当我再次启动我的应用程序时,我会直接进入与 Menu.java 对应的活动,这正是应该发生的事情。但是只有当有人尝试登录时才会发生异常。
请帮帮我!!
提前致谢 :)