在getLocalActivityManager()上获取nullpointer异常.getCurrentActivity()

时间:2013-03-15 15:52:46

标签: android android-activity android-tabactivity

我有一个tabview,它有5个tab.Each,每个都是一个活动。在一个标签中,它打开一个新的活动,打开一个警报。到现在为止,每件事情都可以。

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

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

LogCat错误

enter image description here

家长活动

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

1 个答案:

答案 0 :(得分: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();
    }
}