片段事务出错(空对象参考)

时间:2018-12-25 00:29:05

标签: java android firebase android-mvp

我是 Android开发的新手,并且在代码中遇到一些错误。我正在尝试通过setFragment函数为HomeActivity设置所需的Fragment(仪表板,费用,收入) 。但是我想实现一个MVP方法,所以我创建了Presenter类和Contract接口。我的问题是我无法通过Presenter实现此功能,我也不知道为什么。我试图获取Context,获取Activity,也可以与片段管理器一起使用,但是它不起作用,并且会引发Null指针异常。

HomeActivityContract

package georgiosApp.manager;

import android.support.v4.app.Fragment;

public interface HomeActivityContract
{
interface View {
     void setFragment(Fragment fragment);
}
interface Presenter{
     void setFragment1(Fragment fragment);
       }
}

HomeActivity

package georgiosApp.manager;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;

import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.widget.FrameLayout;

import com.google.firebase.auth.FirebaseAuth;

import georgiosApp.manager.Fragments.DashBoardFragment;
import georgiosApp.manager.Fragments.ExpenseFragment;
import georgiosApp.manager.Fragments.IncomeFragment;
 import georgiosApp.manager.MainActivityMVP.MainActivity;

public class HomeActivity extends AppCompatActivity implements 
NavigationView.OnNavigationItemSelectedListener,HomeActivityContract.View{

    private BottomNavigationView bottomNavigationView;
    private FrameLayout frameLayout;
    public HomeActivityPresenter presenter;

    //Fragment

    private DashBoardFragment dashBoardFragment;
    private IncomeFragment incomeFragment;
    private ExpenseFragment expenseFragment;
    private FirebaseAuth mAuth;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

       InitViews();
        bottomNavigationView.setOnNavigationItemSelectedListener(new 
BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) 
            {
                 switch (item.getItemId()){

                case R.id.dashboard:
                    setFragment(dashBoardFragment);

 bottomNavigationView.setItemBackgroundResource(R.color.colordas);
                    return true;

                case R.id.income:
                    setFragment(incomeFragment);

bottomNavigationView.setItemBackgroundResource(R.color.colorin);
                    return true;

                case R.id.expense:
                    setFragment(expenseFragment);

bottomNavigationView.setItemBackgroundResource(R.color.colorex);
                    return true;

                default:
                    return false;

            }
        }
    });


}
public void InitViews(){
    Toolbar toolbar=findViewById(R.id.my_toolbar);
    toolbar.setTitle("Expense Manager");
    setSupportActionBar(toolbar);
    mAuth=FirebaseAuth.getInstance();
    bottomNavigationView=findViewById(R.id.bottomNavigationbar);
    frameLayout=findViewById(R.id.main_frame);
    DrawerLayout drawerLayout=findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle=new ActionBarDrawerToggle(






    this,drawerLayout,toolbar,
R.string.navigation_drawer_open,R.string.navigation_d 
 rawer_close
);

    drawerLayout.addDrawerListener(toggle);
    toggle.syncState();
    NavigationView navigationView=findViewById(R.id.naView);
    navigationView.setNavigationItemSelectedListener(this);
    dashBoardFragment=new DashBoardFragment();
    incomeFragment=new IncomeFragment();
    expenseFragment=new ExpenseFragment();
    presenter=new 
   HomeActivityPresenter(HomeActivity.this,HomeActivity.this);
   presenter.setFragment1(dashBoardFragment);
    //setFragment(dashBoardFragment);


}

public void setFragment(Fragment fragment) {

    FragmentTransaction 
 fragmentTransaction=fragment.getFragmentManager().beginTransaction();
    fragmentTransaction.replace(R.id.main_frame,fragment);
    fragmentTransaction.commit();


    }

    @Override
    public void onBackPressed() {

    DrawerLayout drawerLayout=findViewById(R.id.drawer_layout);

    if (drawerLayout.isDrawerOpen(GravityCompat.END)){
        drawerLayout.closeDrawer(GravityCompat.END);
    }else {
        super.onBackPressed();
    }


}


   public void displaySelectedListener(int itemId){

    Fragment fragment=null;

    switch (itemId){
        case R.id.dashboard:
            fragment=new DashBoardFragment();
            break;

        case R.id.income:

            fragment=new IncomeFragment();

            break;

        case R.id.expense:
            fragment=new ExpenseFragment();
            break;

        case R.id.logout:
            mAuth.signOut();
            startActivity(new 
 Intent(getApplicationContext(),MainActivity.class));
            break;

    }

    if (fragment!=null){

        FragmentTransaction 
 ft=getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.main_frame,fragment);
        ft.commit();

    }

    DrawerLayout drawerLayout=findViewById(R.id.drawer_layout);
    drawerLayout.closeDrawer(GravityCompat.START);

  }



@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    displaySelectedListener(item.getItemId());
    return true;
}


}

HomeActivityPresenter

package georgiosApp.manager;

import android.app.Activity;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;

import com.google.firebase.auth.FirebaseAuth;

 public class HomeActivityPresenter extends FragmentActivity implements 
 HomeActivityContract.Presenter {
 private FirebaseAuth mAuth;
 public Fragment fragment;
 public Context context;
 public FragmentTransaction fragmentTransaction;
  Activity activity;

public HomeActivityPresenter( Activity activity,Context context) {
    //this.fragment = fragment;
    this.activity=activity;
    this.context=context;
    // this.mAuth=mAuth;
    //this.context=context;

}
public void setFragment1(Fragment fragment) {
    FragmentTransaction 
fragmentTransaction=fragment.getFragmentManager().beginTransaction();
    fragmentTransaction.replace(R.id.main_frame, fragment);
    fragmentTransaction.commit();

}

错误

E/AndroidRuntime: FATAL EXCEPTION: main
Process: georgiosApp.manager, PID: 23647
java.lang.RuntimeException: Unable to start activity ComponentInfo{georgiosApp.manager/georgiosApp.manager.HomeActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.support.v4.app.FragmentTransaction android.support.v4.app.FragmentManager.beginTransaction()' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2927)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2988)
    at android.app.ActivityThread.-wrap14(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1631)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6682)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.support.v4.app.FragmentTransaction android.support.v4.app.FragmentManager.beginTransaction()' on a null object reference at georgiosApp.manager.HomeActivityPresenter.setFragment1(HomeActivityPresenter.java:29)
    at georgiosApp.manager.HomeActivity.InitViews(HomeActivity.java:97)
    at georgiosApp.manager.HomeActivity.onCreate(HomeActivity.java:47)
    at android.app.Activity.performCreate(Activity.java:6942)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2880)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2988) 
    at android.app.ActivityThread.-wrap14(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1631) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6682) at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:152) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410) 
 W/DynamiteModule: Local module descriptor class for 
com.google.firebase.auth not found.


enter code here

2 个答案:

答案 0 :(得分:0)

将所有from tkinter import * from random import randint INITIAL_STAKE = 100 money = INITIAL_STAKE def adjust_money(value): global money money += value money_var.set(money) def reset_money(): global money money = INITIAL_STAKE money_var.set(money) output.delete(0.0, END) def start_round(): output.delete(0.0, END) output.insert(0.0, 'Here are your numbers:\n') for _ in range(0, 3): number = randint(0, 20) output.insert(END, number) if number == 20: output.insert(END, '\nYou won!\n') adjust_money(50) else: output.insert(END, '\nYou Lost!\n') adjust_money(-10) def exit_window(): root.destroy() exit() root = Tk() root.title('Gamble game') root.configure(background='black') money_var = IntVar() money_var.set(money) output = Text(root, width=50, height=8, wrap=WORD) output.grid(row=1, column=0, columnspan=3, sticky=W) Label(root, textvariable=money_var, bg='black', fg='white').grid(row=0, column=0, sticky=W) Button(root, text='Play', width=10, command=start_round).grid(row=2, column=0, sticky=W) Button(root, text='Restart', width=10, command=reset_money).grid(row=2, column=1) Button(root, text='Exit', width=10, command=exit_window).grid(row=2, column=2, sticky=E) root.mainloop() 更改为getFragmentManager()。 如果不起作用,请将所有getSupportFragmentManager()更改为getSupportFragmentManager()

答案 1 :(得分:0)

设置片段应该在活动内部而不是在演示者中进行。 演示者不应依赖于Android框架。这样做会破坏MVP的规则,并使演示者无法测试。

我建议进一步阅读MVP,并研究各种实现方式,例如google示例中的实现方式 todo-mvp

此外,还有许多有关Android MVP的文章可以为您提供帮助。

basics-of-mvp-the-android-way

model-view-presenter-android-guidelines

architectural-guidelines-to-follow-for-mvp-pattern-in-android