我的导航抽屉是用占位符视图构建的,但是当我按下注销按钮时,它不会启动我想在注销后启动的活动。 这是代码,有人可以向我解释我做错了吗?
由于
@Layout(R.layout.drawer_item)
public class DrawerMenuItem {
public static final int DRAWER_MENU_ITEM_LOGOUT = 8;
private int mMenuPosition;
private Context mContext;
private DrawerCallBack mCallBack;
public DrawerMenuItem(Context context, int menuPosition) {
mContext = context;
mMenuPosition = menuPosition;
}
@Resolve
private void onResolved() {
switch (mMenuPosition){
case DRAWER_MENU_ITEM_LOGOUT:
itemIcon.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ic_exit_to_app_black_24dp));
itemNameTxt.setText("Logout");
break;
}
}
@Click(R.id.mainView)
private void onMenuItemClick(){
switch (mMenuPosition){
case DRAWER_MENU_ITEM_LOGOUT:
Toast.makeText(mContext, "Logout", Toast.LENGTH_SHORT).show();
if(mCallBack != null)mCallBack.onLogoutMenuSelected();
mContext.startActivity(new Intent (mContext,MainActivity.class));
break;
}
}
public void setDrawerCallBack(DrawerCallBack callBack) {
mCallBack = callBack;
}
public interface DrawerCallBack{
void onLogoutMenuSelected();
}
}
这是我的清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.albej.orederveg">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<!-- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> -->
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".AddFood"
android:windowSoftInputMode="adjustResize"/>
<activity android:name=".OpenOrders" />
<activity android:name=".LoginActivity" />
<activity android:name=".FarmerMenuActivity" />
<activity android:name=".DetailsActivity" />
<activity android:name=".ItemListActivity" />
<activity android:name=".SingleFoodActivity" />
<activity android:name=".SingleOrderActivity" />
<activity android:name=".MainTabActivity"></activity>
</application>
</manifest>
这是我想要在退出时开始的主要活动
public class MainActivity extends AppCompatActivity {
private EditText email,pass,loc,name;
private DatabaseReference mDatabase;
private FirebaseAuth mAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
email=(EditText) findViewById(R.id.editEmail);
pass=(EditText) findViewById(R.id.editPass);
mAuth= FirebaseAuth.getInstance();
mDatabase= FirebaseDatabase.getInstance().getReference().child("Farmers");
}
//Signing up
public void signupButtonClicked(View view){
final String email_text=email.getText().toString().trim();
String pass_text=pass.getText().toString().trim();
if(!TextUtils.isEmpty(email_text) && !TextUtils.isEmpty(pass_text)){
mAuth.createUserWithEmailAndPassword(email_text,pass_text).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
String user_id = mAuth.getCurrentUser().getUid();
DatabaseReference current_user = mDatabase.child(user_id);
current_user.child("email").setValue(email_text)
Intent detailsIntent = new Intent(MainActivity.this, DetailsActivity.class);
startActivity(detailsIntent);
}
}
});
}
}
public void signinButtonClicked(View view){
Intent loginIntent=new Intent(MainActivity.this,LoginActivity.class);
startActivity(loginIntent);
}
}
在Logcat下面
01-27 18:15:37.953 10376-10376/com.example.albej.orederveg W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
01-27 18:15:52.743 10376-10574/com.example.albej.orederveg W/System.err: remove failed: ENOENT (No such file or directory) : /data/user/0/com.example.albej.orederveg/cache/picasso-cache/journal.tmp
01-27 18:16:37.193 10376-10376/com.example.albej.orederveg D/ViewRootImpl: ViewPostImeInputStage processPointer 0
01-27 18:16:37.293 10376-10376/com.example.albej.orederveg D/ViewRootImpl: ViewPostImeInputStage processPointer 1
01-27 18:16:37.313 10376-10376/com.example.albej.orederveg W/System.err: java.lang.reflect.InvocationTargetException
01-27 18:16:37.313 10376-10376/com.example.albej.orederveg W/System.err: at java.lang.reflect.Method.invoke(Native Method)
01-27 18:16:37.313 10376-10376/com.example.albej.orederveg W/System.err: at com.mindorks.placeholderview.ViewBinder$1.onClick(ViewBinder.java:159)
01-27 18:16:37.313 10376-10376/com.example.albej.orederveg W/System.err: at android.view.View.performClick(View.java:5697)
01-27 18:16:37.313 10376-10376/com.example.albej.orederveg W/System.err: at android.view.View$PerformClick.run(View.java:22526)
01-27 18:16:37.313 10376-10376/com.example.albej.orederveg W/System.err: at android.os.Handler.handleCallback(Handler.java:739)
01-27 18:16:37.313 10376-10376/com.example.albej.orederveg W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
01-27 18:16:37.313 10376-10376/com.example.albej.orederveg W/System.err: at android.os.Looper.loop(Looper.java:158)
01-27 18:16:37.313 10376-10376/com.example.albej.orederveg W/System.err: at android.app.ActivityThread.main(ActivityThread.java:7224)
01-27 18:16:37.313 10376-10376/com.example.albej.orederveg W/System.err: at java.lang.reflect.Method.invoke(Native Method)
01-27 18:16:37.313 10376-10376/com.example.albej.orederveg W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
01-27 18:16:37.313 10376-10376/com.example.albej.orederveg W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
01-27 18:16:37.313 10376-10376/com.example.albej.orederveg W/System.err: Caused by: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
01-27 18:16:37.313 10376-10376/com.example.albej.orederveg W/System.err: at android.app.ContextImpl.startActivity(ContextImpl.java:734)
01-27 18:16:37.313 10376-10376/com.example.albej.orederveg W/System.err: at android.app.ContextImpl.startActivity(ContextImpl.java:721)
01-27 18:16:37.313 10376-10376/com.example.albej.orederveg W/System.err: at android.content.ContextWrapper.startActivity(ContextWrapper.java:345)
01-27 18:16:37.313 10376-10376/com.example.albej.orederveg W/System.err: at com.example.albej.orederveg.DrawerMenuItem.onMenuItemClick(DrawerMenuItem.java:120)
01-27 18:16:37.313 10376-10376/com.example.albej.orederveg W/System.err: ... 11 more
01-27 18:16:37.323 10376-10376/com.example.albej.orederveg D/SecWifiDisplayUtil: Metadata value : none
01-27 18:16:37.323 10376-10376/com.example.albej.orederveg D/ViewRootImpl: #1 mView = android.widget.LinearLayout{fd99f00 V.E...... ......I. 0,0-0,0 #10203a7 android:id/toast_layout_root}
01-27 18:16:37.373 10376-10376/com.example.albej.orederveg D/ViewRootImpl: MSG_RESIZED_REPORT: ci=Rect(0, 0 - 0, 0) vi=Rect(0, 0 - 0, 0) or=1
01-27 18:16:37.393 10376-10376/com.example.albej.orederveg W/DisplayListCanvas: DisplayListCanvas is started on unbinded RenderNode (without mOwningView)
01-27 18:16:39.333 10376-10376/com.example.albej.orederveg D/ViewRootImpl: #3 mView = null
01-27 18:17:39.763 10376-10634/com.example.albej.orederveg V/FA: Recording user engagement, ms: 107315
01-27 18:17:39.773 10376-10634/com.example.albej.orederveg V/FA: Using measurement service
01-27 18:17:39.773 10376-10634/com.example.albej.orederveg V/FA: Connecting to remote service
01-27 18:17:39.783 10376-10634/com.example.albej.orederveg V/FA: Activity paused, time: 181999657
01-27 18:17:39.793 10376-10634/com.example.albej.orederveg D/FA: Logging event (FE): user_engagement(_e), Bundle[{firebase_event_origin(_o)=auto, engagement_time_msec(_et)=107315, firebase_screen_class(_sc)=MainTabActivity, firebase_screen_id(_si)=6299809065178216245}]
01-27 18:17:39.803 10376-10634/com.example.albej.orederveg V/FA: Using measurement service
01-27 18:17:39.803 10376-10634/com.example.albej.orederveg V/FA: Connection attempt already in progress
01-27 18:17:39.863 10376-10376/com.example.albej.orederveg V/ActivityThread: updateVisibility : ActivityRecord{3b9ab2c token=android.os.BinderProxy@fc50a4f {com.example.albej.orederveg/com.example.albej.orederveg.MainTabActivity}} show : true
01-27 18:17:39.863 10376-10634/com.example.albej.orederveg D/FA: Connected to remote service
01-27 18:17:39.863 10376-10634/com.example.albej.orederveg V/FA: Processing queued up service tasks: 2
01-27 18:17:44.913 10376-10634/com.example.albej.orederveg V/FA: Inactivity, disconnecting from the service
01-27 18:20:51.153 10376-10428/com.example.albej.orederveg W/DynamiteModule: Local module descriptor class for com.google.firebase.auth not found.
答案 0 :(得分:2)
更换
MainTabActivity maintab=new MainTabActivity();
maintab.Logout();
与
SaveSharedPreference.clearUserName(mContext);
mContext.startActivity(new Intent (mContext, MainActivity.class));
应该解决问题。如果您希望在sepparate方法中使用这部分代码,则应将该方法保留在DrawerMenuItem
中。
如果它仍然无法正常工作,请在点击按钮时检查Logcat中是否存在任何内容,并将AndroidManifest.xml
和MainActivity.java
中的代码添加到您的问题中。
<强>更新强>
从LogCat日志的这一行中可以清楚地看到问题:
从Activity上下文外部调用startActivity()需要 FLAG_ACTIVITY_NEW_TASK标志。这真的是你想要的吗?
这意味着mContext
不是活动的上下文。
要解决此问题,请确保在创建DrawerMenuItem
时使用Activity的上下文。在你有这样的行:new DrawerMenuItem(context, menuPosition)
,context
需要是活动。
答案 1 :(得分:0)
而不是这个:-
case DRAWER_MENU_ITEM_LOGOUT:
Toast.makeText(mContext, "Logout", Toast.LENGTH_SHORT).show();
if(mCallBack != null)mCallBack.onLogoutMenuSelected();
mContext.startActivity(new Intent (mContext,MainActivity.class));
break;
写这个:-
case DRAWER_MENU_ITEM_LOGOUT:
Intent intent = new Intent(mContext,MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(intent);
Toast.makeText(mContext, "Logout", Toast.LENGTH_SHORT).show();
if(mCallBack != null)mCallBack.onLogoutMenuSelected();
break;