gpt4 book ai didi

java - fragment 事务出错(空对象引用)

转载 作者:行者123 更新时间:2023-11-29 02:23:10 26 4
gpt4 key购买 nike

我是 Android 开发 的新手,我的代码出现了一些错误。我正在尝试通过 setFragment 函数为我的 HomeActivity 设置 Fragment(dashboard,expense,income) .但我想实现一个 MVP 方法所以我创建了 Presenter 类和 Contract 接口(interface)。我的问题是我不能通过演示者实现这个功能,我不知道为什么。我试图获取上下文,获取 Activity ,也玩 fragment 管理器,但它不起作用,它会抛出空指针异常。

首页 Activity 合约

package georgiosApp.manager;

import android.support.v4.app.Fragment;

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

主页 Activity

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

最佳答案

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

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com