我正在尝试在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;
}
这是Manifest代码:
<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相对应的活动,这正是应该发生的事情。但只有当有人试图登录时才会发生异常 请帮帮我!! 在此先感谢:)
答案 0 :(得分:0)
打开/启动其他活动的意图就像
Intent launchAnotherActivity = new Intent(CurrentActivity.this,
AnotherActivity.class);
startActivity(launchAnotherActivity);
所以,我不认为你写的是正确的。