无法将数据从活动传递到片段

时间:2017-09-26 06:55:20

标签: android class android-fragments

我正在处理一个应用,其中我有一个function cleanComponentList(curSS) { Logger.log("Init script cleanComponentList 'Department_List'") curSS = curSS || SpreadsheetApp.getActiveSpreadsheet() var deptListStart = curSS.getRangeByName("Department_List") if(deptListStart==null){ Logger.log("This script cannot be used without the named range 'Department_List'") return } var dept = deptListStart.offset(1,0) while( ! dept.isBlank() ){ Logger.log("Started processing " + dept.getDisplayValue() + " Dept.") //Get Department Sheet var deptSht = curSS.getSheetByName(dept.offset(0,1).getValue()) //Get Column that has Action Summary in department sheet var actSummCol=deptSht.getRange(2, 1) //Get Department Input Sheet (Target Sheet where we need to copy the final values to var deptInputSht = curSS.getSheetByName(dept.offset(0,2).getValue()) //Find column with the "Column Header"/"Summary" in which we search #N/A while( ! (actSummCol.getDisplayValue() === dept.offset(0,3).getDisplayValue())){ var tmp = actSummCol.getDisplayValue() var actColRng = actSummCol.getA1Notation() actSummCol=actSummCol.offset(0,1) } var actSummColAddress = actSummCol.getA1Notation(); Logger.log("Found key \"" + dept.offset(0,3).getDisplayValue() + "\" for " + dept.getDisplayValue() + " @ " + actSummColAddress) var errRows = [] //Get all rows that have error Action Summary var lastRow = deptSht.getLastRow() for(nRow=1;nRow<lastRow;nRow++){ //There should be a better of checking #N/A if(actSummCol.offset(nRow,0).getDisplayValue().equals("#N/A")){ errRows.push(nRow+actSummCol.getRow()) } } Logger.log("Got " + errRows.length + " error rows for " + dept.getDisplayValue() + " Dept.") //Get Cell where data append should start from. var deptInputLastAvailableRow = deptInputSht.getRange("B1") while(!deptInputLastAvailableRow.isBlank() || deptInputLastAvailableRow.isPartOfMerge()){ deptInputLastAvailableRow=deptInputLastAvailableRow.offset(1,0) } Logger.log(dept.getDisplayValue() + " Dept." + " Input will be updated from " + deptInputLastAvailableRow.getA1Notation()) //Copy CDE from the filtered rows to Department Input Sheet var srcCols=deptSht.getRange(dept.offset(0,4).getDisplayValue()) //There should be a better way of iterating. for-of throws syntax error!!! for(idx in errRows){ var row = errRows[idx]; var lc = srcCols.getLastColumn() var fc = srcCols.getColumn() var errKeyRangeAddress=srcCols.getCell(row,1).getA1Notation() + ":" + srcCols.getCell(row,lc-fc + 1).getA1Notation() var errKeyRange=deptSht.getRange(errKeyRangeAddress) errKeyRange.copyTo(deptInputLastAvailableRow) deptInputLastAvailableRow=deptInputLastAvailableRow.offset(1,0) } Logger.log("Copied " + errRows.length + " entries to Sheet \"" + deptInputSht.getName() + "\"") Logger.log("Finished Processing " + dept.getDisplayValue() + " Dept.") dept=dept.offset(1,0) } } function onOpen(){ //Choose which way you want your menu to appear. //SpreadsheetApp.getActiveSpreadsheet().addMenu("Monitor", [{name:"Clean Up Keys",functionName:"cleanComponentList"}]) SpreadsheetApp.getUi().createAddonMenu().addItem("Clean Up Keys", "cleanComponentList").addToUi() } ,最初加载Activity。我在活动中获得IMEI No,Latitude,Longitude并将该数据传递到片段中并将片段中的数据获取但是当我获取数据时我得到空指针异常。

最初我的帐户活动加载注册片段,我也从帐户活动发送数据到注册片段

//从

发送数据的活动代码

//在帐户活动中设置SignUp片段

Fragment

//现在调用函数GeoLocation并将其发送到注册片段

changeFragments(new SignupFragment());

//现在注册片段&gt;我正在把这些字段称为Api

public void GeoLocation() {

    Bundle bundle = new Bundle();
    bundle.putString("imei_no", imei_number);
    bundle.putString("lat",latitude.toString());
    bundle.putString("long",longitude.toString());
    bundle.putString("postal_code",postalCode);

    SignupFragment fragObj = new SignupFragment();
    fragObj.setArguments(bundle);
} 

我在这里得到Null Pointer异常。

2 个答案:

答案 0 :(得分:0)

changeFragments(fragobj);

更改此行,因为如果您当时使用新的SingupFragment,则会创建新对象

答案 1 :(得分:0)

你应该传递片段的实例,而不是创建一个像: -

  public void GeoLocation() {

      Bundle bundle = new Bundle();
            bundle.putString("imei_no", imei_number);
            bundle.putString("lat",latitude.toString());
            bundle.putString("long",longitude.toString());
            bundle.putString("postal_code",postalCode);

            SignupFragment fragobj = new SignupFragment();
            fragobj.setArguments(bundle);

  changeFragments(fragobj);
    } 

这里我调用GeoLocation中的changeFragments()方法。