我正在调试我的应用程序,并收到一个“奇怪”的异常。它不一致,因此很难解决。我将在下面发布一些代码。
给出异常的类:
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();
}
}
异常本身:
09-30 08:44:12.571: ERROR/AndroidRuntime(10557): FATAL EXCEPTION: main
09-30 08:44:12.571: ERROR/AndroidRuntime(10557): java.lang.NullPointerException
09-30 08:44:12.571: ERROR/AndroidRuntime(10557): at com.dezide.android.troubleshooter.view.TabGroupActivity.finishFromChild(TabGroupActivity.java:46)
09-30 08:44:12.571: ERROR/AndroidRuntime(10557): at android.app.Activity.finish(Activity.java:3290)
09-30 08:44:12.571: ERROR/AndroidRuntime(10557): at com.dezide.android.troubleshooter.view.TabGroupActivity.onBackPressed(TabGroupActivity.java:106)
09-30 08:44:12.571: ERROR/AndroidRuntime(10557): at com.dezide.android.troubleshooter.view.TabGroupActivity.onKeyUp(TabGroupActivity.java:93)
09-30 08:44:12.571: ERROR/AndroidRuntime(10557): at android.view.KeyEvent.dispatch(KeyEvent.java:1281)
09-30 08:44:12.571: ERROR/AndroidRuntime(10557): at android.app.Activity.dispatchKeyEvent(Activity.java:2075)
09-30 08:44:12.571: ERROR/AndroidRuntime(10557): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1673)
09-30 08:44:12.571: ERROR/AndroidRuntime(10557): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:796)
09-30 08:44:12.571: ERROR/AndroidRuntime(10557): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:796)
09-30 08:44:12.571: ERROR/AndroidRuntime(10557): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:796)
09-30 08:44:12.571: ERROR/AndroidRuntime(10557): at android.widget.TabHost.dispatchKeyEvent(TabHost.java:275)
09-30 08:44:12.571: ERROR/AndroidRuntime(10557): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:796)
09-30 08:44:12.571: ERROR/AndroidRuntime(10557): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:796)
09-30 08:44:12.571: ERROR/AndroidRuntime(10557): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:1697)
09-30 08:44:12.571: ERROR/AndroidRuntime(10557): at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1111)
09-30 08:44:12.571: ERROR/AndroidRuntime(10557): at android.app.Activity.dispatchKeyEvent(Activity.java:2070)
09-30 08:44:12.571: ERROR/AndroidRuntime(10557): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1673)
09-30 08:44:12.571: ERROR/AndroidRuntime(10557): at android.view.ViewRoot.deliverKeyEventToViewHierarchy(ViewRoot.java:2493)
09-30 08:44:12.571: ERROR/AndroidRuntime(10557): at android.view.ViewRoot.handleFinishedEvent(ViewRoot.java:2463)
09-30 08:44:12.571: ERROR/AndroidRuntime(10557): at android.view.ViewRoot.handleMessage(ViewRoot.java:1752)
09-30 08:44:12.571: ERROR/AndroidRuntime(10557): at android.os.Handler.dispatchMessage(Handler.java:99)
09-30 08:44:12.571: ERROR/AndroidRuntime(10557): at android.os.Looper.loop(Looper.java:144)
09-30 08:44:12.571: ERROR/AndroidRuntime(10557): at android.app.ActivityThread.main(ActivityThread.java:4937)
09-30 08:44:12.571: ERROR/AndroidRuntime(10557): at java.lang.reflect.Method.invokeNative(Native Method)
09-30 08:44:12.571: ERROR/AndroidRuntime(10557): at java.lang.reflect.Method.invoke(Method.java:521)
09-30 08:44:12.571: ERROR/AndroidRuntime(10557): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-30 08:44:12.571: ERROR/AndroidRuntime(10557): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-30 08:44:12.571: ERROR/AndroidRuntime(10557): at dalvik.system.NativeStart.main(Native Method)
问题是它并不总是在同一个地方失败。有时我可以在异常发生前按回 20 次,有时只有 5 次。
编辑:添加更多代码。
My GuideActivity,这是您执行步骤并将每个步骤作为新 Activity 启动的地方:
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;
}
我的开始 Activity :
public void onCreate( Bundle savedInstanceState )
{
super.onCreate( savedInstanceState );
try
{
populateSections();
}
catch( Exception e )
{
e.printStackTrace();
}
}
private void populateSections() throws Exception
{
setContentView( R.layout.sections );
ArrayList<XmlSection> sections = new ArrayList<XmlSection>();
Portal portal = model.getPortal();
sections = portal.getSections();
final ArrayList<XmlSection> tempSections = sections;
ListView listView = ( ListView ) findViewById( R.id.sectionList );
listView.setAdapter( new CustomAdapter( this, R.layout.list_item, sections ) );
listView.setTextFilterEnabled( true );
listView.setOnItemClickListener( new OnItemClickListener()
{
public void onItemClick( AdapterView<?> parent, View view,
int position, long id )
{
try
{
Intent i = new Intent().setClass( SectionActivity.this, JAndroidTroubleshooterActivity.class );
i.putExtra( "name", tempSections.get( position ).getName() );
startActivity( i );
}
catch( Exception e )
{
e.printStackTrace();
}
}
});
}
我的主要 Activity 启动的 Activity :
@Override
public void onCreate( Bundle savedInstanceState )
{
super.onCreate( savedInstanceState );
Resources res = getResources();
TabHost tabHost = getTabHost();
TabHost.TabSpec spec;
Intent intent;
intent = new Intent().setClass( this, TabGroupGuideActivity.class );
intent.putExtra( "name", getIntent().getStringExtra( "name" ) );
spec = tabHost.newTabSpec( "guides" ).setIndicator( "Guides",
res.getDrawable( R.drawable.guides ) )
.setContent( intent );
tabHost.addTab( spec );
intent = new Intent().setClass( this, TabGroupFaqActivity.class );
intent.putExtra( "name", getIntent().getStringExtra( "name" ) );
spec = tabHost.newTabSpec( "faqs" ).setIndicator( "FAQs",
res.getDrawable( R.drawable.faq ) )
.setContent( intent );
tabHost.addTab( spec );
}
private Activity getCurrentTab()
{
Activity activity = getLocalActivityManager().getActivity( getTabHost().getCurrentTabTag() );
return activity;
}
private void optionRestart()
{
if ( getCurrentTab() instanceof TabGroupFaqActivity )
{
Intent i = new Intent().setClass( this, FaqActivity.class );
TabGroupFaqActivity activity = ( TabGroupFaqActivity ) getCurrentActivity();
i.putExtra( "name", activity.getCurrentSection() );
activity.startChildActivity( "restart", i );
}
if ( getCurrentTab() instanceof TabGroupGuideActivity )
{
Intent i = new Intent().setClass( this, GuideListActivity.class );
TabGroupGuideActivity activity = ( TabGroupGuideActivity ) getCurrentActivity();
i.putExtra( "name", activity.getCurrentSection() );
activity.startChildActivity( "restart", i );
}
}
private void optionFeedback()
{
if ( getCurrentTab() instanceof TabGroupFaqActivity )
{
Activity currentTab = ( ( TabGroupFaqActivity ) getCurrentTab() ).getCurrentActivity();
Article currentArticle = ( ( FaqActivity ) currentTab ).getCurrentArticle();
if ( currentArticle != null )
{
Intent i = new Intent().setClass( this, FeedbackActivity.class );
i.putExtra( "perform", "feedbackfaq" );
i.putExtra( "title", currentArticle.getTitle() );
startActivity( i );
}
else
{
Toast toast = Toast.makeText( getApplicationContext(), "You have to select an article to submit feedback", 500 );
toast.show();
}
}
if ( getCurrentTab() instanceof TabGroupGuideActivity )
{
Activity currentTab = ( ( TabGroupGuideActivity ) getCurrentTab() ).getCurrentActivity();
GuideStep currentStep = ( ( GuideActivity ) currentTab ).getCurrentStep();
if ( currentStep != null && "false".equalsIgnoreCase( currentStep.getTerminalStep() ) )
{
Intent i = new Intent().setClass( this, FeedbackActivity.class );
i.putExtra( "perform", "feedbackguide" );
i.putExtra( "session", currentStep.getSession() );
i.putExtra( "action", currentStep.getStepTitle() );
i.putExtra( "title", currentStep.getGuideTitle() );
startActivity( i );
}
else
{
Toast toast = Toast.makeText( getApplicationContext(), "You cannot submit feedback for the last step of a guide", 500 );
toast.show();
}
}
}
private void optionHome()
{
Intent i = new Intent().setClass( this, SectionActivity.class );
i.setFlags( Intent.FLAG_ACTIVITY_CLEAR_TOP );
startActivity( i );
}
@Override
public void onConfigurationChanged( Configuration newConfig )
{
super.onConfigurationChanged( newConfig );
}
@Override
public boolean onCreateOptionsMenu( Menu menu )
{
MenuInflater inflater = getMenuInflater();
inflater.inflate( R.menu.menu, menu );
return true;
}
@Override
public boolean onOptionsItemSelected( MenuItem item )
{
switch ( item.getItemId() )
{
case R.id.menuRestart:
optionRestart();
return true;
case R.id.menuHome:
optionHome();
return true;
case R.id.menuFeedback:
optionFeedback();
return true;
default:
return super.onOptionsItemSelected( item );
}
}
我的标签组 Activity :
@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;
}
最后,我列出指南的 Activity :
public void onCreate(Bundle savedInstanceState) { super.onCreate(saveInstanceState);
try
{
populateGuideList( getIntent().getStringExtra( "name" ) );
}
catch( Exception e )
{
e.printStackTrace();
}
}
public void populateGuideList( String name ) throws Exception
{
setContentView( R.layout.guides );
Section section = model.getSection( name );
guides = section.getGuides();
ListView listView = ( ListView ) findViewById( R.id.guideList );
listView.setAdapter( new CustomAdapter( this, R.layout.list_item, guides ) );
listView.setTextFilterEnabled( true );
listView.setOnItemClickListener( new OnItemClickListener()
{
public void onItemClick( AdapterView<?> parent, View view,
int position, long id )
{
try
{
XmlGuide guide = guides.get( position );
Intent i = new Intent().setClass( getParent(), GuideActivity.class );
i.putExtra( "guide", guide.getName() );
i.putExtra( "action", "startguide" );
TabGroupActivity parentActivity = ( TabGroupActivity )getParent();
parentActivity.startChildActivity( guide.getName(), i );
}
catch( Exception e )
{
e.printStackTrace();
}
}
});
}
最佳答案
我认为这可能有帮助:
public void startChildActivity(String id, Intent intent) {
id+=System.currentTimeMillis();
//rest of your code.
}
编辑:它需要一些详细说明。
有Activity A , B和 C
A ID "A" B ID "B" C ID "C" B 的另一个实例ID "B" (它可能
发生,因为B可能是一个 ResultListScreen,它可能需要
多次使用“加载更多”类型按钮)问题是在ArrayList<String> idList当你完成最后一个B
manager.destroyActivity( idList.get( index ), true );
调用的行破坏了Activity有 id "B" , 堆栈中有 2 个。
两者 B被删除和C被推。当你完成 C并且没有 B在堆栈中但它的 Id 可用是 ArrayList<String> idList你得到它的Activity这是 null如果没有找到。
就这些。
id+=System.currentTimeMillis();
在这行代码中,您每次开始新的Activity时都会分配一个唯一的ID通过使用系统的当前时间。
关于android - 返回 Activity 时出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7607039/
我正在用Ruby编写一个简单的程序来检查域列表是否被占用。基本上它循环遍历列表,并使用以下函数进行检查。require'rubygems'require'whois'defcheck_domain(domain)c=Whois::Client.newc.query("google.com").available?end程序不断出错(即使我在google.com中进行硬编码),并打印以下消息。鉴于该程序非常简单,我已经没有什么想法了-有什么建议吗?/Library/Ruby/Gems/1.8/gems/whois-2.0.2/lib/whois/server/adapters/base.
我想为Heroku构建一个Rails3应用程序。他们使用Postgres作为他们的数据库,所以我通过MacPorts安装了postgres9.0。现在我需要一个postgresgem并且共识是出于性能原因你想要pggem。但是我对我得到的错误感到非常困惑当我尝试在rvm下通过geminstall安装pg时。我已经非常明确地指定了所有postgres目录的位置可以找到但仍然无法完成安装:$envARCHFLAGS='-archx86_64'geminstallpg--\--with-pg-config=/opt/local/var/db/postgresql90/defaultdb/po
为什么4.1%2返回0.0999999999999996?但是4.2%2==0.2。 最佳答案 参见此处:WhatEveryProgrammerShouldKnowAboutFloating-PointArithmetic实数是无限的。计算机使用的位数有限(今天是32位、64位)。因此计算机进行的浮点运算不能代表所有的实数。0.1是这些数字之一。请注意,这不是与Ruby相关的问题,而是与所有编程语言相关的问题,因为它来自计算机表示实数的方式。 关于ruby-为什么4.1%2使用Ruby返
我有一个包含多个键的散列和一个字符串,该字符串不包含散列中的任何键或包含一个键。h={"k1"=>"v1","k2"=>"v2","k3"=>"v3"}s="thisisanexamplestringthatmightoccurwithakeysomewhereinthestringk1(withspecialcharacterslike(^&*$#@!^&&*))"检查s是否包含h中的任何键的最佳方法是什么,如果包含,则返回它包含的键的值?例如,对于上面的h和s的例子,输出应该是v1。编辑:只有字符串是用户定义的。哈希将始终相同。 最佳答案
我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file
所以我开始关注ruby,很多东西看起来不错,但我对隐式return语句很反感。我理解默认情况下让所有内容返回self或nil但不是语句的最后一个值。对我来说,它看起来非常脆弱(尤其是)如果你正在使用一个不打算返回某些东西的方法(尤其是一个改变状态/破坏性方法的函数!),其他人可能最终依赖于一个返回对方法的目的并不重要,并且有很大的改变机会。隐式返回有什么意义?有没有办法让事情变得更简单?总是有返回以防止隐含返回被认为是好的做法吗?我是不是太担心这个了?附言当人们想要从方法中返回特定的东西时,他们是否经常使用隐式返回,这不是让你组中的其他人更容易破坏彼此的代码吗?当然,记录一切并给出
为什么以下不同?Time.now.end_of_day==Time.now.end_of_day-0.days#falseTime.now.end_of_day.to_s==Time.now.end_of_day-0.days.to_s#true 最佳答案 因为纳秒数不同:ruby-1.9.2-p180:014>(Time.now.end_of_day-0.days).nsec=>999999000ruby-1.9.2-p180:015>Time.now.end_of_day.nsec=>999999998
在Ruby1.9.3(可能还有更早的版本,不确定)中,我试图弄清楚为什么Ruby的String#split方法会给我某些结果。我得到的结果似乎与我的预期相反。这是一个例子:"abcabc".split("b")#=>["a","ca","c"]"abcabc".split("a")#=>["","bc","bc"]"abcabc".split("c")#=>["ab","ab"]在这里,第一个示例返回的正是我所期望的。但在第二个示例中,我很困惑为什么#split返回零长度字符串作为返回数组的第一个值。这是什么原因呢?这是我所期望的:"abcabc".split("a")#=>["bc"
我正在使用Postgres.app在OSX(10.8.3)上。我已经修改了我的PATH,以便应用程序的bin文件夹位于所有其他文件夹之前。Rammy:~phrogz$whichpg_config/Applications/Postgres.app/Contents/MacOS/bin/pg_config我已经安装了rvm并且可以毫无错误地安装pggem,但是当我需要它时我得到一个错误:Rammy:~phrogz$gem-v1.8.25Rammy:~phrogz$geminstallpgFetching:pg-0.15.1.gem(100%)Buildingnativeextension
我最近对我的计算机(OS-MacOSX10.6.8)进行了删除,并且我正在重新安装我所有的开发工具。我再次安装了RVM;但是,它不会让我安装Ruby1.9.3。到目前为止我已经尝试过:rvminstall1.9.3rvm安装1.9.3-p194rvm安装1.9.3-p448rvminstall1.9.3--with-gcc=clang所有返回相同的命令行错误:Searchingforbinaryrubies,thismighttakesometime.Nobinaryrubiesavailablefor:osx/10.6/x86_64/ruby-1.9.3-p448.Continuin