0

我有一个有 5 个选项卡的选项卡视图。每个选项卡都是一个活动。在一个选项卡中,它正在打开一个新活动,该活动打开一个警报。直到现在一切正常。

我的问题是当我从(后退按钮按下)返回时,它正在调用它的父活动的 onBackPressed 子活动,所以我正在检查活动名称getLocalActivityManager().getCurrentActivity().toString(),当我从子活动按下后退按钮时它也可以工作,它检查活动名称使用getLocalActivityManager().getCurrentActivity().toString().

但是当我从我的父活动中按下返回按钮时,它会给出 NullPointer 异常。getLocalActivityManager().getCurrentActivity().toString()从父活动调用时可能无法正常工作

LogCat 错误

在此处输入图像描述

家长活动

public class ProgressActivity extends ActivityGroup 
{
private ArrayList<View> history;
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.progress_tabview);
    this.history = new ArrayList<View>();
}
public void showProgress(View v) 
{
    Intent intMyProg = new Intent(this,ShowProgress.class);
    //intMyProg.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    //startActivity(intMyProg);

    //This will help to open a new activity within existing TabView
    //Need to extend ActivityGroup
    //Need to declare "private ArrayList<View> history;"
    //Need to Define "this.history = new ArrayList<View>();" within onCreate
    View MyView = getLocalActivityManager()
            .startActivity("MyIntent", intMyProg
            .addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP))
            .getDecorView();


    replaceView(MyView);
}

public void replaceView(View v) 
{
    history.add(v);
    setContentView(v);
}

//@Override  
public void onBackPressed() 
{  
    String activityName = getLocalActivityManager().getCurrentActivity().toString();

    if(activityName.contains("ShowProgress"))
    {
        getCurrentActivity().onBackPressed();
    }
    else
    {
        Toast.makeText(getApplicationContext(), "Back button from parent Activity", Toast.LENGTH_SHORT).show();
        //finish();
    }
}
}

儿童活动

public class ShowProgress extends ActivityGroup
{
private ArrayList<View> history;
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.progress);
    this.history = new ArrayList<View>();

    AlertDialog alertDialog = new AlertDialog.Builder(ShowProgress.this.getParent()).create();
    alertDialog.setTitle("Hello ...");
    alertDialog.setMessage("Hello world this is Anirban !");
    alertDialog.setIcon(R.drawable.tick);
    alertDialog.setButton("OK", new DialogInterface.OnClickListener() 
    {
        public void onClick(DialogInterface dialog, int which) 
        {
            //Toast.makeText(getApplicationContext(), "You clicked on OK", Toast.LENGTH_SHORT).show();
        }
    });
    alertDialog.show();
}

//@Override  
public void onBackPressed() 
{  
    Intent intMyProg = new Intent(this,ProgressActivity.class);
    View MyView = getLocalActivityManager()
            .startActivity("MyIntent", intMyProg
            .addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP))
            .getDecorView();

    replaceView(MyView);
}

public void replaceView(View v) 
{
    history.add(v);
    setContentView(v);
}
 }
4

1 回答 1

2

由于您在此过程中遇到错误,因此摆脱它的最简单方法是像这样更改它

//@Override  

public void onBackPressed() 
{  
    LocalActivityManager localManager = getLocalActivityManager();
    Activity currentActivity = null;
    if (localManager!=null){
          currentActivity = localManager.getCurrentActivity();
    }

    String activityName = null;
    if (currentActivity!=null){
      activityName = currentActivity.toString()
    }

    if(activityName!=null && (activityName.contains("ShowProgress"))
    {
        getCurrentActivity().onBackPressed();
    }
    else
    {
        Toast.makeText(getApplicationContext(), "Back button from parent Activity", Toast.LENGTH_SHORT).show();
        //finish();
    }
}
于 2013-03-15T17:12:37.150 回答