Android 返回活动时出现NullPointerException
我正在调试我的应用程序,并得到一个“奇怪”的异常。这是不一致的,因此很难解决。我将在下面发布一些代码 给出我的异常的类:Android 返回活动时出现NullPointerException,android,nullpointerexception,Android,Nullpointerexception,我正在调试我的应用程序,并得到一个“奇怪”的异常。这是不一致的,因此很难解决。我将在下面发布一些代码 给出我的异常的类: private ArrayList<String> idList; @Override public void onCreate( Bundle savedInstanceState ) { super.onCreate( savedInstanceState ); if ( idList == null ) { idLi
private ArrayList<String> idList;
@Override
public void onCreate( Bundle savedInstanceState )
{
super.onCreate( savedInstanceState );
if ( idList == null )
{
idList = new ArrayList<String>();
}
}
@Override
public void finishFromChild( Activity child )
{
LocalActivityManager manager = getLocalActivityManager();
int index = idList.size()-1;
if ( index < 1 )
{
finish();
return;
}
manager.destroyActivity( idList.get( index ), true );
idList.remove( index ); index--;
String lastId = idList.get( index );
Activity lastActivity = manager.getActivity( lastId );
Intent lastIntent = lastActivity.getIntent();
Window newWindow = manager.startActivity( lastId, lastIntent );
setContentView( newWindow.getDecorView() );
}
public void startChildActivity( String id, Intent intent )
{
if ( "restart".equalsIgnoreCase( id ) )
{
idList.clear();
}
Window window = getLocalActivityManager().startActivity( id, intent.addFlags( Intent.FLAG_ACTIVITY_CLEAR_TOP ) );
if ( window != null )
{
idList.add( id );
setContentView( window.getDecorView() );
}
}
public Activity getCurrentActivity()
{
int length = idList.size();
if ( idList.isEmpty() ) {
return null;
}
else
{
return getLocalActivityManager().getActivity( idList.get( length-1 ) );
}
}
@Override
public boolean onKeyDown( int keyCode, KeyEvent event )
{
if ( keyCode == KeyEvent.KEYCODE_BACK )
{
return true;
}
return super.onKeyDown( keyCode, event );
}
@Override
public boolean onKeyUp( int keyCode, KeyEvent event )
{
if ( keyCode == KeyEvent.KEYCODE_BACK )
{
onBackPressed();
return true;
}
return super.onKeyUp( keyCode, event );
}
@Override
public void onBackPressed()
{
int length = idList.size();
if ( length > 1 )
{
Activity current = getLocalActivityManager().getActivity( idList.get( length-1 ) );
current.finish();
}
}
问题是它并不总是在同一个地方失败。有时我可以在异常发生前按20次,有时只有5次
编辑:添加更多代码
My GuideActivity(我的指南活动),您可以在其中制定步骤,并将每个步骤作为新活动启动:
public void onCreate( Bundle savedInstanceState )
{
super.onCreate( savedInstanceState );
if ( "startguide".equalsIgnoreCase( getIntent().getStringExtra( "action" ) ) )
{
try
{
startGuide( getIntent().getStringExtra( "guide" ) );
}
catch( Exception e )
{
e.printStackTrace();
}
}
else if ( "nextstep".equalsIgnoreCase( getIntent().getStringExtra( "action" ) ) )
{
String session = getIntent().getStringExtra( "session" );
String step = getIntent().getStringExtra( "step" );
String response = getIntent().getStringExtra( "response" );
try
{
nextGuideStep( session, step, response );
}
catch( Exception e )
{
e.printStackTrace();
}
}
}
public GuideStep getCurrentStep()
{
return currentStep;
}
public void setCurrentStep( GuideStep currentStep )
{
this.currentStep = currentStep;
}
public void startGuide( String name ) throws Exception
{
GuideStep guideStep = model.startGuide( name );
currentStep = guideStep;
setContentView ( R.layout.main );
LinearLayout linearLayout = ( LinearLayout ) findViewById( R.id.linearLayout );
TextView stepTitle = ( TextView ) findViewById( R.id.stepTitle );
TextView explanation = ( TextView ) findViewById( R.id.explanation );
stepTitle.setText( guideStep.getStepTitle() );
if ( guideStep.getExplanation() != "" )
{
explanation.setText( Html.fromHtml( guideStep.getExplanation() ) );
}
responses = guideStep.getResponses();
ListView listView = new ListView( this );
linearLayout.addView( listView );
listView.setAdapter( new CustomAdapter( this, R.layout.list_item, responses ) );
listView.setTextFilterEnabled( true );
listView.setOnItemClickListener( new OnItemClickListener()
{
public void onItemClick( AdapterView<?> parent, View view,
int position, long id ) {
try
{
Intent i = new Intent().setClass( getParent(), GuideActivity.class );
i.putExtra( "action", "nextstep" );
i.putExtra( "session", currentStep.getSession() );
i.putExtra( "step", currentStep.getStep() );
i.putExtra( "response", currentStep.getResponse( position ).getId() );
TabGroupActivity parentActivity = ( TabGroupActivity )getParent();
parentActivity.startChildActivity( currentStep.getStepTitle(), i );
}
catch( Exception e )
{
e.printStackTrace();
}
}
});
}
public void nextGuideStep( String session, String step, String responseId ) throws Exception
{
GuideStep guideStep = model.nextGuideStep( session, step, responseId );
currentStep = guideStep;
setContentView ( R.layout.main );
LinearLayout linearLayout = ( LinearLayout ) findViewById( R.id.linearLayout );
TextView stepTitle = ( TextView ) findViewById( R.id.stepTitle );
TextView explanation = ( TextView ) findViewById( R.id.explanation );
TextView didThisAnswerYourQuestion = ( TextView ) findViewById( R.id.didThisAnswerYourQuestion );
ScrollView scrollView = ( ScrollView ) findViewById( R.id.scrollView );
stepTitle.setText( guideStep.getStepTitle() );
if ( guideStep.getExplanation() != "" )
{
explanation.setText( Html.fromHtml( guideStep.getExplanation() ) );
}
responses = guideStep.getResponses();
if ( guideStep.getStepTitle() != "" )
{
ListView listView = new ListView( this );
linearLayout.addView( listView );
listView.setAdapter( new CustomAdapter( this, R.layout.list_item, responses ) );
listView.setTextFilterEnabled( true );
listView.setOnItemClickListener( new OnItemClickListener() {
public void onItemClick( AdapterView<?> parent, View view,
int position, long id ) {
try
{
Intent i = new Intent().setClass( getParent(), GuideActivity.class );
i.putExtra( "action", "nextstep" );
i.putExtra( "session", currentStep.getSession() );
i.putExtra( "step", currentStep.getStep() );
i.putExtra( "response", currentStep.getResponse( position ).getId() );
TabGroupActivity parentActivity = ( TabGroupActivity )getParent();
parentActivity.startChildActivity( currentStep.getStepTitle(), i );
}
catch( Exception e )
{
e.printStackTrace();
}
}
});
}
else {
stepTitle.setVisibility( View.GONE );
didThisAnswerYourQuestion.setVisibility( View.GONE );
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT );
params.addRule( RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE );
params.height = 350;
scrollView.setLayoutParams( params );
}
}
public boolean availableForFeedback()
{
return currentStep != null;
}
@Override
public void onCreate( Bundle savedInstanceState )
{
super.onCreate( savedInstanceState );
currentSection = getIntent().getStringExtra( "name" );
startChildActivity( "GuideListActivity",
new Intent( this, GuideListActivity.class ).putExtra( "name", getIntent().getStringExtra( "name" ) ) );
}
public String getCurrentSection()
{
return currentSection;
}
我的选项卡组活动:
public void onCreate( Bundle savedInstanceState )
{
super.onCreate( savedInstanceState );
if ( "startguide".equalsIgnoreCase( getIntent().getStringExtra( "action" ) ) )
{
try
{
startGuide( getIntent().getStringExtra( "guide" ) );
}
catch( Exception e )
{
e.printStackTrace();
}
}
else if ( "nextstep".equalsIgnoreCase( getIntent().getStringExtra( "action" ) ) )
{
String session = getIntent().getStringExtra( "session" );
String step = getIntent().getStringExtra( "step" );
String response = getIntent().getStringExtra( "response" );
try
{
nextGuideStep( session, step, response );
}
catch( Exception e )
{
e.printStackTrace();
}
}
}
public GuideStep getCurrentStep()
{
return currentStep;
}
public void setCurrentStep( GuideStep currentStep )
{
this.currentStep = currentStep;
}
public void startGuide( String name ) throws Exception
{
GuideStep guideStep = model.startGuide( name );
currentStep = guideStep;
setContentView ( R.layout.main );
LinearLayout linearLayout = ( LinearLayout ) findViewById( R.id.linearLayout );
TextView stepTitle = ( TextView ) findViewById( R.id.stepTitle );
TextView explanation = ( TextView ) findViewById( R.id.explanation );
stepTitle.setText( guideStep.getStepTitle() );
if ( guideStep.getExplanation() != "" )
{
explanation.setText( Html.fromHtml( guideStep.getExplanation() ) );
}
responses = guideStep.getResponses();
ListView listView = new ListView( this );
linearLayout.addView( listView );
listView.setAdapter( new CustomAdapter( this, R.layout.list_item, responses ) );
listView.setTextFilterEnabled( true );
listView.setOnItemClickListener( new OnItemClickListener()
{
public void onItemClick( AdapterView<?> parent, View view,
int position, long id ) {
try
{
Intent i = new Intent().setClass( getParent(), GuideActivity.class );
i.putExtra( "action", "nextstep" );
i.putExtra( "session", currentStep.getSession() );
i.putExtra( "step", currentStep.getStep() );
i.putExtra( "response", currentStep.getResponse( position ).getId() );
TabGroupActivity parentActivity = ( TabGroupActivity )getParent();
parentActivity.startChildActivity( currentStep.getStepTitle(), i );
}
catch( Exception e )
{
e.printStackTrace();
}
}
});
}
public void nextGuideStep( String session, String step, String responseId ) throws Exception
{
GuideStep guideStep = model.nextGuideStep( session, step, responseId );
currentStep = guideStep;
setContentView ( R.layout.main );
LinearLayout linearLayout = ( LinearLayout ) findViewById( R.id.linearLayout );
TextView stepTitle = ( TextView ) findViewById( R.id.stepTitle );
TextView explanation = ( TextView ) findViewById( R.id.explanation );
TextView didThisAnswerYourQuestion = ( TextView ) findViewById( R.id.didThisAnswerYourQuestion );
ScrollView scrollView = ( ScrollView ) findViewById( R.id.scrollView );
stepTitle.setText( guideStep.getStepTitle() );
if ( guideStep.getExplanation() != "" )
{
explanation.setText( Html.fromHtml( guideStep.getExplanation() ) );
}
responses = guideStep.getResponses();
if ( guideStep.getStepTitle() != "" )
{
ListView listView = new ListView( this );
linearLayout.addView( listView );
listView.setAdapter( new CustomAdapter( this, R.layout.list_item, responses ) );
listView.setTextFilterEnabled( true );
listView.setOnItemClickListener( new OnItemClickListener() {
public void onItemClick( AdapterView<?> parent, View view,
int position, long id ) {
try
{
Intent i = new Intent().setClass( getParent(), GuideActivity.class );
i.putExtra( "action", "nextstep" );
i.putExtra( "session", currentStep.getSession() );
i.putExtra( "step", currentStep.getStep() );
i.putExtra( "response", currentStep.getResponse( position ).getId() );
TabGroupActivity parentActivity = ( TabGroupActivity )getParent();
parentActivity.startChildActivity( currentStep.getStepTitle(), i );
}
catch( Exception e )
{
e.printStackTrace();
}
}
});
}
else {
stepTitle.setVisibility( View.GONE );
didThisAnswerYourQuestion.setVisibility( View.GONE );
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT );
params.addRule( RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE );
params.height = 350;
scrollView.setLayoutParams( params );
}
}
public boolean availableForFeedback()
{
return currentStep != null;
}
@Override
public void onCreate( Bundle savedInstanceState )
{
super.onCreate( savedInstanceState );
currentSection = getIntent().getStringExtra( "name" );
startChildActivity( "GuideListActivity",
new Intent( this, GuideListActivity.class ).putExtra( "name", getIntent().getStringExtra( "name" ) ) );
}
public String getCurrentSection()
{
return currentSection;
}
最后,我的活动列表指导:
创建时的公共void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState)
试试看
{
populateGuideList(getIntent().getStringExtra(“名称”);
}
捕获(例外e)
{
e、 printStackTrace();
}
}
public void populateGuideList(字符串名称)引发异常
{
setContentView(R.layout.guides);
Section=model.getSection(名称);
guides=section.getGuides();
ListView ListView=(ListView)findViewById(R.id.guideList);
setAdapter(新的CustomAdapter(this,R.layout.list_项,guides));
setTextFilterEnabled(true);
setOnItemClickListener(新的OnItemClickListener()
{
public void onItemClick(AdapterView父级、视图、,
内部位置,长id)
{
尝试
{
XmlGuide=guides.get(位置);
Intent i=new Intent().setClass(getParent(),GuideActivity.class);
i、 putExtra(“guide”,guide.getName());
i、 putExtra(“行动”、“开始指南”);
TabGroupActivity parentActivity=(TabGroupActivity)getParent();
parentActivity.startChildActivity(guide.getName(),i);
}
捕获(例外e)
{
e、 printStackTrace();
}
}
});
}
NullPointerException主要发生在您试图指向不存在的内容时……例如:您忘记在资源文件夹中添加图像,并说是setBackground(图像)或者您忘了在清单文件中提到某个活动,正试图使用Intent访问该活动。请确保您的res文件夹中有所有资源,并且清单文件中提到的所有活动都在,我认为这可能会有所帮助:
public void startChildActivity(String id, Intent intent) {
id+=System.currentTimeMillis();
//rest of your code.
}
编辑:它需要一点细化
有活动
A
、B
和C
- 您可以使用Id
“A”启动
A
- 然后用Id
“B”启动
B
- 然后用Id
“C”启动
C
- 然后从C再次启动另一个id为
的“B”
实例(它可能 发生,因为B
可能是一个可能需要 多次使用“加载更多类型”按钮)B
ArrayList idList
中,当您从上一个B
manager.destroyActivity( idList.get( index ), true );
调用一行,该行销毁id为“B”的活动,这是堆栈中的2。
同时删除B
,并按下C
。当您完成C
并且堆栈中没有B
,但其Id可用时,它的ArrayList idList
,您将获得它的活动
,如果找不到它,它的活动
就这些
id+=System.currentTimeMillis();
在这行代码中,每次使用系统的当前时间开始新的活动时,您都会分配一个唯一的id。我在这里找到了更好的解决方案:
如果您正在执行id+System.currentTimeMillis()
操作,您可能会遇到以下问题:
有时a+b=c+d,所以你仍然获得NPE的可能性很小
例如,您需要将id存储在某个位置才能关闭它
您应该包含整个代码,但如果没有。TabGroupActivity.java中的第46行是什么?第46行是Intent lastIntent=lastActivity.getIntent();我将继续添加一些代码:)事情是这样的,正如我提到的。它在崩溃前可能工作了20次,这让我相信还有别的东西。它也不总是在同一步发生崩溃。您似乎在使用putExtra传递一些参数。检查您是否在各自的活动中使用Bundle接收它们(我正在这样做:)问题似乎是,如果您第一次回答“未知”,则同一步骤可能会发生两次。使我的唯一id不再那么独特。当您按下back键并销毁具有给定id的活动时,您将同时销毁这两个活动,使其在堆栈中的较早位置到达同一活动时不可用。这似乎解决了问题。是什么让你认为这解决了问题?我想这就是问题所在。我没有考虑过,因为我真的不知道它会发生两次。但谢谢你的帮助:)标记为解决问题的答案。
manager.destroyActivity( idList.get( index ), true );
id+=System.currentTimeMillis();