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

家庭活动合同

package georgiosApp.manager;

import android.support.v4.app.Fragment;

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


家庭活动

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;
}


}


家庭活动演示者

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

最佳答案

将所有getFragmentManager()更改为getSupportFragmentManager()
如果不起作用,请将所有getSupportFragmentManager()更改为getFragmentManager()

关于java - fragment 事务出错(空对象引用),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53918545/

10-08 23:53