0

嗨,我正在開發一個應用程序在Android中,我已經嘗試在Communicator.generateMessageId()中的空對象引用上調用vitrual方法。Android應用程序的空對象引用

這裏是我的代碼發生錯誤:

public class StatsActivity extends TaxiplonActivity { 

protected Communicator comm; 
protected TaxiMapGoogle messageId; 
Message statusMessageBody; 

public void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.stats_activity); 
    ToggleButton toggleButton = (ToggleButton) findViewById (R.id.toggleButton1); 

    // get action bar 
    ActionBar actionBar = getActionBar(); 


    actionBar.setDisplayHomeAsUpEnabled(true); 
    actionBar.setDisplayShowHomeEnabled(false); 
    actionBar.setTitle("Driver Options"); 

    testRequest(); 
} 
@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.menu.option_menu, menu); 

    return super.onCreateOptionsMenu(menu); 
} 

public void testRequest() { 
    sendMessage(comm.generateMessageId(),"agent=driver&action=get&driverid=6609", 
      OPTION_DRIVER, activityHandler,null); 

    //System.out.println("MessageId: "+comm.generateMessageId()); 

} 
@Override 
public void postConnectionSuccessful() { 
    // TODO Auto-generated method stub 

} 
@Override 
public void preConnectionReset() { 
    // TODO Auto-generated method stub 

} 

}

,這裏是我從我的通訊器類調用方法:

public String generateMessageId() { 

    try { 
     TaxiplonApp taxiplonApp = (TaxiplonApp) parent 
       .getApplicationContext(); 
     TwoFishEncryprion twoFishEncryprion = new TwoFishEncryprion(
       messageIdKey); 
     String cleanMessageId = taxiplonApp.getDriverId() + "|" 
       + taxiplonApp.getDeviceId() + "|" 
       + ntpClock.getCachedDate().getTime(); 
     return twoFishEncryprion.encrypt(cleanMessageId).replace(" ", ""); 
    } catch (NTPClockException e) { 
     AploonLogger.error("Problem generating imei messageId", e); 
     parent.showNtpClockDialog(); 
    } 

    return null; 
} 

我initiallize在COMM我TaxiplonActivity.class here:

protected class Connector extends AsyncTask<Runnable, Void, Void> { 
    @Override 
    protected void onPreExecute() { 
     showBusyDialog(); 
    } 
    @Override 
    protected Void doInBackground(Runnable... runnables) { 
     //init communication 
     comm = Communicator.getInstance(TaxiplonActivity.this, false); 
     //if connection is already available, proceed to postConnectionInit (it will not be called, like when connection is established) 
     if(comm.isConnected()) { 
      postConnectionSuccessful(); 
     } 
     return null; 
    } 
    @Override 
    protected void onPostExecute(Void v) { 
     hideBusyDialog(); 
    } 
} 

任何人都可以幫助我如何解決這個錯誤?

+1

你在哪裏初始化'comm'? – poss

+0

'comm'初始化在哪裏? – Piyush

+0

我更新了我的問題,告訴你我在哪裏初始化它 – appLogic

回答

1

移動這個代碼從doInBackground()方法

comm = Communicator.getInstance(TaxiplonActivity.this, false); 

之前

testRequest(); 

方法。

因爲您的testRequest()在您的comm初始化之前執行。所以總是你得到null實例。

+0

@PG很長一段時間見你... + 1 –

+1

感謝哥們。同樣在這裏。你可以請來聊天室 – Piyush

+0

聊天室????好的,我知道了。哪一個??? –